xml数据:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:padd<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Spinner android:id="@+id/spinner_city" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>ingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Spinner android:id="@+id/spinner_city" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>
源代码:
package com.example.day04_work1; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.util.Xml; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Spinner; /** * 将文件weathercity.xml数据中的省份显示在下拉列表中 * 1.将文件中的数据读取出来 * 2.解析数据,将数据存到集合(数据源) * 3.下拉列表设置适配器,将集合作为数据源放入适配器 */ public class MainActivity extends Activity { private Spinner spinner; private String TAG = "info"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner_city); String data = getStringFromFile(); if(data != null && data.length() != 0){ //准备数据源 List<String> list = parsexmlToList(data); //创建适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, list); //设置适配器 spinner.setAdapter(adapter); } } /** * 解析xml数据到集合 * @param data * @return */ private List<String> parsexmlToList(String data) { // TODO Auto-generated method stub try { List<String> list = new ArrayList<String>(); //获取xml解析器 /** * 三种方式: * 1.在res目录中的xml目录中的xml文件:XmlPullParser parser = getResource().getXml(R.xml.citys_weather.xml); * 2.XmlPullParser parser = Xml.newPullParser(); * 3.XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); */ XmlPullParser parser = Xml.newPullParser(); //设置数据源 parser.setInput(new StringReader(data)); //得到事件码 int code = parser.getEventType(); while((code!=XmlPullParser.END_DOCUMENT)){ String name = parser.getName(); switch (code) { case XmlPullParser.START_TAG: if("pn".equals(name)){ list.add(parser.nextText()); } break; default: break; } code = parser.next(); } return list; } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 从文件获取xml数据 * @return */ private String getStringFromFile() { InputStream is = null; ByteArrayOutputStream baos = null; try { //将asserts目录中的文件转换成输入流 is = getResources().getAssets().open("citys_weather.xml"); baos = new ByteArrayOutputStream(); int len = 0; byte[] buf = new byte[1024*1024]; while(-1!=(len = is.read(buf))){ baos.write(buf, 0, len); baos.flush(); } return new String(baos.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(baos!=null){ baos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(is!=null){ is.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }