Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之解析XML並實現三級聯動效果

Android開發之解析XML並實現三級聯動效果

編輯:關於Android編程

請尊重他人的勞動成果,轉載請注明出處:Android開發之解析XML並實現三級聯動效果

本實例主要應用XmlPullParser解析XML文檔中的省市區,然後將數據綁定到Spinner上實現三級聯動的效果。關於XmlPullParser的詳解大家可以參考《Android開發之使用PULL解析和生成XML》一文。

運行效果圖:

省市區三級聯動省市區三級聯動省市區三級聯動

程序代碼:

核心代碼:

package com.jph.sevice;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;

/**
 * 解析省、市、區xml
 * @author jph
 * Date:2014.09.25
 */
public class PullProvince {	
	public static final int PARSESUCCWSS=0x2001;
	private Handler handler;
	public PullProvince(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler=handler;
	}
	/**
	 * 獲取所有省份城市以及區	
	 * @author jph
	 * Date:2014.09.25	
	 */
	public void getProvinces(final InputStream inStream) {			
		new Thread(new Runnable() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					XmlPullParser pullParser = Xml.newPullParser();
					pullParser.setInput(inStream, "UTF-8");
					int event;
						event = pullParser.getEventType();
					Map>> provinces = new HashMap>>();// 省份
					Map> cities=null;// 城市		
					ArrayList areaAll = null;
					String pName = "";// 省份名稱
					String cName = "";// 城市名稱
					String aName = "";// 地區名
					String targetName = "";// 當前節點的名稱
					while (event != XmlPullParser.END_DOCUMENT) {
						targetName = pullParser.getName();
						switch (event) {
						case XmlPullParser.START_TAG:
							if ("province".equals(targetName)) {// 處理省份節點				
								pName = pullParser.getAttributeValue(0);// 當前省份名
								cities=new HashMap>();
							} else if ("city".equals(targetName)) {// 處理城市節點				
								cName = pullParser.getAttributeValue(0);
								areaAll = new ArrayList();// 地區
							} else if ("area".equals(targetName)) {// 處理地區節點
								aName = pullParser.getAttributeValue(0);
							}
							break;
						case XmlPullParser.END_TAG:
							if (targetName.equals("area")) {
								areaAll.add(aName);
							} else if (targetName.equals("city")) {
								cities.put(cName, areaAll);
							} else if (targetName.equals("province")) {
								provinces.put(pName, cities);
							}
							break;
						}
						event = pullParser.next();
					}
					Message message=new Message();					
					message.obj=provinces;//將解析出的數據放到message中傳給主線程
					message.what=PARSESUCCWSS;
					handler.sendMessage(message);//通知主線程數據解析完畢
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}
		}).start();		
	}
}


Activity:

package com.jph.px;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import com.jph.sevice.PullProvince;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
/**
 * 省,市,區三級聯動
 * @author jph
 * Date:2014.09.25
 */
public class MainActivity extends Activity {
	private Spinner province,city,area;	
	private Map>> data=null;
	/**當前的選擇的省份**/
	private	String currentProvince;
	/**當前的選擇的城市**/
	private	String currentCity;
	private PullProvince pullProvince;	
//	/**當前的選擇的區**/
//	private	String currentArea;
	private Handler mHandler=new Handler(){
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case PullProvince.PARSESUCCWSS://數據解析完畢,加載數據
				data=(Map>>) msg.obj;
				initData();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		province=(Spinner)findViewById(R.id.province);
		city=(Spinner)findViewById(R.id.city);
		area=(Spinner)findViewById(R.id.area);	
		pullProvince=new PullProvince(mHandler);
		InputStream inStream = this.getClass().getClassLoader().
				getResourceAsStream("province_city.xml");		
	    pullProvince.getProvinces(inStream);			
	}	
	/**
	 * 初始化數據
	 */
	private void initData(){		
		if (data!=null) {
			String[]arrStrings= data.keySet().toArray(new String[0]);
			System.out.println(arrStrings);			
			//將省份信息填充到 province Spinner
			province.setAdapter(new ArrayAdapter(this, 
					android.R.layout.simple_list_item_multiple_choice,arrStrings));
			currentProvince=getCurrentProvince();	
			bindCityAdapter(currentProvince);
			currentCity=getCurrentCity();			
			bindAreaAdapter(currentCity);			
			setOnItemSelectedListener();			
		}
	}
	private void bindAreaAdapter(String currentCity) {
		// TODO Auto-generated method stub
		//根據當前顯示的城市將對應的區填充到area Spinner		
		area.setAdapter(new ArrayAdapter(this, 
				android.R.layout.simple_list_item_multiple_choice,
				data.get(currentProvince).get(currentCity)));
	}
	private void bindCityAdapter(String currentProvince) {
		// TODO Auto-generated method stub
		//根據當前顯示的省份將對應的城市填充到city Spinner
		city.setAdapter(new ArrayAdapter(this, 
				android.R.layout.simple_list_item_multiple_choice,data.
				get(currentProvince).keySet().toArray(new String[0])));
	}
	/**
	 * 為Spinner設置監聽器
	 */
	private void setOnItemSelectedListener() {
		// TODO Auto-generated method stub
		province.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				currentProvince=getCurrentProvince();
				bindCityAdapter(currentProvince);
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		city.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				currentCity=getCurrentCity();
				bindAreaAdapter(currentCity);
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		area.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this,area.getSelectedItem().toString(), 
						Toast.LENGTH_SHORT).show();	
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {
				// TODO Auto-generated method stub
				
			}
		});	
	}

	/**獲取當前選擇的省份
	 * @return String:當前選擇的省份
	 */
	private String getCurrentProvince() {
		// TODO Auto-generated method stub
		return province.getSelectedItem().toString();
	}
	/**獲取當前選擇的城市
	 * @return String:當前選擇的城市
	 */
	private String getCurrentCity() {
		// TODO Auto-generated method stub
		return city.getSelectedItem().toString();
	}
}


布局文件:


	
	
	
	








  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved