android 获取国家信息,如何解析本地xml文件获取国家详细信息android?

这里是解析Xml文件的代码,您将不得不通过本地xml文件的inputstream。

public static ArrayList parseCountry(Context context, InputStream inputStream) {

String KEY = "";

String VALUE = null;

ArrayList arrCountires = new ArrayList();

Country country = null;

ArrayList arrStates = null;

State state= null;

ArrayList arrCities = null;

City city = null;

try {

InputStreamReader inputreader = null;

if(inputStream != null) {

inputreader = new InputStreamReader(inputStream);

}

if(inputreader != null) {

XmlPullParserFactory factory = null;

factory = XmlPullParserFactory.newInstance();

factory.setNamespaceAware(true);

XmlPullParser xpp = null;

xpp = factory.newPullParser();

xpp.setInput(inputreader);

int eventType = 0;

eventType = xpp.getEventType();

eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {

if(eventType == XmlPullParser.START_TAG) {

KEY = xpp.getName();

if(KEY.equalsIgnoreCase(TAGS.COUNTRIES)) {

arrCountires = new ArrayList();

}else if(KEY.equalsIgnoreCase(TAGS.COUNTRY)) {

country = new Country();

arrStates = new ArrayList();

country.setCountryId(xpp.getAttributeValue(null, TAGS.ID));

}else if(KEY.equalsIgnoreCase(TAGS.STATE)) {

state = new State();

arrCities = new ArrayList();

state.setStateId(xpp.getAttributeValue(null, TAGS.ID));

}else if(KEY.equalsIgnoreCase(TAGS.CITY)) {

city = new City();

city.setCityId(xpp.getAttributeValue(null, TAGS.ID));

}

}else if(eventType == XmlPullParser.END_TAG) {

KEY = xpp.getName();

if(KEY.equalsIgnoreCase(TAGS.COUNTRY)) {

country.setArrStates(arrStates);

arrCountires.add(country);

}else if(KEY.equalsIgnoreCase(TAGS.COUNTRY_NAME)) {

country.setCountryName(VALUE);

}else if(KEY.equalsIgnoreCase(TAGS.STATE_NAME)) {

state.setStateName(VALUE);

}else if(KEY.equalsIgnoreCase(TAGS.STATE)) {

state.setArrCities(arrCities);

arrStates.add(state);

}else if(KEY.equalsIgnoreCase(TAGS.CITY)) {

arrCities.add(city);

}else if(KEY.equalsIgnoreCase(TAGS.CITY_NAME)) {

city.setCityName(VALUE);

}

}else if(eventType == XmlPullParser.TEXT) {

VALUE = xpp.getText();

}

eventType = xpp.next();

}

}

}

catch (Exception e) {

e.printStackTrace();

}finally {

if(inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return arrCountires;

}

这里是一个乡村类getter和setter方法。

public class Country {

String countryId;

String countryName;

ArrayList arrStates;

public ArrayList getArrStates() {

return arrStates;

}

public void setArrStates(ArrayList arrStates) {

this.arrStates = arrStates;

}

public String getCountryId() {

return countryId;

}

public void setCountryId(String countryId) {

this.countryId = countryId;

}

public String getCountryName() {

return countryName;

}

public void setCountryName(String countryName) {

this.countryName = countryName;

}

}

这里是一个适配器类设置的国家在微调。

private class CountryAdapter implements SpinnerAdapter{

ArrayList data;

public CountryAdapter(ArrayList data){

this.data = data;

}

@Override

public int getCount() {

return data.size();

}

@Override

public Object getItem(int position) {

return data.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public int getItemViewType(int position) {

return android.R.layout.simple_spinner_dropdown_item;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

TextView v = new TextView(getApplicationContext());

v.setTextColor(Color.BLACK);

v.setText(data.get(position).getName());

v.setTextSize(15);

v.setPadding(10, 10, 10, 10);

v.setSingleLine();

v.setEllipsize(TruncateAt.END);

return v;

}

@Override

public int getViewTypeCount() {

return android.R.layout.simple_spinner_item;

}

@Override

public boolean hasStableIds() {

return false;

}

@Override

public boolean isEmpty() {

return false;

}

@Override

public void registerDataSetObserver(DataSetObserver observer) {

}

@Override

public void unregisterDataSetObserver(DataSetObserver observer) {

}

@Override

public View getDropDownView(int position, View convertView,

ViewGroup parent) {

return this.getView(position, convertView, parent);

}

}

这里是一个接口,通过它可以从旋转获得所选国家

OnItemSelectedListener OnCountrySelected = new OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView> arg0, View v, int position,

long id) {

if(position != AdapterView.INVALID_POSITION) {

System.out.println("Country name = " + arrCountries.get(position).getName());

//Here you can set this value to the textview

}

}

@Override

public void onNothingSelected(AdapterView> arg0) {

}

};

这里有一个方法,你如何设置监听器以微调

spCountry.setOnItemSelectedListener(OnCountrySelected);

这是打开文件作为inputstre的代码是从资产

try {

InputStream inputStream = v.getContext().getAssets().open("path of file");

ArrayList arrCountries = parseCountry(this, inputStream);

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

当你拿到国家则设置适配器的阵列中的微调

CountryAdapter countryAdapter = new CountryAdapter(arrCountry);

spCountry.setAdapter(countryAdapter); 响应

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值