基本信息
源码名称:android 省市区3级联动
源码大小:2.63M
文件格式:.zip
开发语言:Java
更新时间:2016-07-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


package com.example.choosecity;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

 
 
 

/**
 * 
 * @author zhy
 * 
 */
public class CitiesActivity extends Activity implements OnWheelChangedListener
{
	/**
	 * 锟斤拷全锟斤拷锟绞★拷锟斤拷锟斤拷锟斤拷息锟斤拷json锟侥革拷式锟斤拷锟芥,锟斤拷锟斤拷锟斤拷珊锟街滴猲ull
	 */
	private JSONObject mJsonObj;
	/**
	 * 省锟斤拷WheelView锟截硷拷
	 */
	private WheelView mProvince;
	/**
	 * 锟叫碉拷WheelView锟截硷拷
	 */
	private WheelView mCity;
	/**
	 * 锟斤拷锟絎heelView锟截硷拷
	 */
	private WheelView mArea;

	/**
	 * 锟斤拷锟斤拷省
	 */
	private String[] mProvinceDatas;
	/**
	 * key - 省 value - 锟斤拷s
	 */
	private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
	/**
	 * key - 锟斤拷 values - 锟斤拷s
	 */
	private Map<String, String[]> mAreaDatasMap = new HashMap<String, String[]>();

	/**
	 * 锟斤拷前省锟斤拷锟斤拷锟�
	 */
	private String mCurrentProviceName;
	/**
	 * 锟斤拷前锟叫碉拷锟斤拷锟�
	 */
	private String mCurrentCityName;
	/**
	 * 锟斤拷前锟斤拷锟斤拷锟斤拷
	 */
	private String mCurrentAreaName ="";
	String sb;
    SharedPreferences  preferences;
    Map<Integer, Boolean> provinceMap ; 
    Map<Integer, Boolean> cityMap ; 
    Map<Integer, Boolean> areaMap ; 
    
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.citys);
		provinceMap = new HashMap<Integer, Boolean>();
		cityMap = new HashMap<Integer, Boolean>();
		areaMap = new HashMap<Integer, Boolean>();
		initJsonData();
        preferences=getSharedPreferences("wwwww", 0);
		mProvince = (WheelView) findViewById(R.id.id_province);
		mCity = (WheelView) findViewById(R.id.id_city);
		mArea = (WheelView) findViewById(R.id.id_area);

		initDatas();

		mProvince.setViewAdapter(new ArrayWheelAdapter<String>(this, mProvinceDatas,0));
		for (int i = 0; i < mProvinceDatas.length; i  ) {
			provinceMap.put(i, false);
			if (i==0){
				provinceMap.put(i, true);
			}
		}
		// 锟斤拷锟絚hange锟铰硷拷
		mProvince.addChangingListener(this);
		// 锟斤拷锟絚hange锟铰硷拷
		mCity.addChangingListener(this);
		// 锟斤拷锟絚hange锟铰硷拷
		mArea.addChangingListener(this);

		mProvince.setVisibleItems(5);
		mCity.setVisibleItems(5);
		mArea.setVisibleItems(5);
		updateCities(0);
		updateAreas(0);
		
		
	}

	/**
	 * 锟斤拷莸锟角帮拷锟斤拷校锟斤拷锟斤拷锟斤拷锟絎heelView锟斤拷锟斤拷息
	 */
	private void updateAreas(int position)
	{
		int pCurrent = mCity.getCurrentItem();
		mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
		String[] areas = mAreaDatasMap.get(mCurrentCityName);

		if (areas == null)
		{
			areas = new String[] { "" };
		}
		mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas,position));
		
		
		for (int i = 0; i < areas.length; i  ) {
			areaMap.put(i, false);
			if (i==position){
				areaMap.put(i, true);
			}
		}
		mArea.setCurrentItem(0);
		sb=areas[0];
		Log.e("222222", sb);
		preferences.edit().putString("sb", sb).commit();
	}

	/**
	 * 锟斤拷莸锟角帮拷锟绞★拷锟斤拷锟斤拷锟斤拷锟絎heelView锟斤拷锟斤拷息
	 */
	private void updateCities(int position)
	{
		int pCurrent = mProvince.getCurrentItem();
		mCurrentProviceName = mProvinceDatas[pCurrent];
		String[] cities = mCitisDatasMap.get(mCurrentProviceName);
		if (cities == null)
		{
			cities = new String[] { "" };
		}
		mCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities,position));
		
		
		
		
		mCity.setCurrentItem(0);
//		updateAreas(0);
	}

	/**
	 * 锟斤拷锟斤拷锟斤拷锟絁son锟斤拷锟斤拷锟斤拷珊锟斤拷头锟絁son锟斤拷锟斤拷锟斤拷诖锟�
	 */
	private void initDatas()
	{
		try
		{
			JSONArray jsonArray = mJsonObj.getJSONArray("citylist");
			mProvinceDatas = new String[jsonArray.length()];
			for (int i = 0; i < jsonArray.length(); i  )
			{
				JSONObject jsonP = jsonArray.getJSONObject(i);// 每锟斤拷省锟斤拷json锟斤拷锟斤拷
				String province = jsonP.getString("p");// 省锟斤拷锟斤拷

				mProvinceDatas[i] = province;

				JSONArray jsonCs = null;
				try
				{
					/**
					 * Throws JSONException if the mapping doesn't exist or is
					 * not a JSONArray.
					 */
					jsonCs = jsonP.getJSONArray("c");
				} catch (Exception e1)
				{
					continue;
				}
				String[] mCitiesDatas = new String[jsonCs.length()];
				for (int j = 0; j < jsonCs.length(); j  )
				{
					JSONObject jsonCity = jsonCs.getJSONObject(j);
					String city = jsonCity.getString("n");// 锟斤拷锟斤拷锟斤拷
					mCitiesDatas[j] = city;
					JSONArray jsonAreas = null;
					try
					{
						/**
						 * Throws JSONException if the mapping doesn't exist or
						 * is not a JSONArray.
						 */
						jsonAreas = jsonCity.getJSONArray("a");
					} catch (Exception e)
					{
						continue;
					}

					String[] mAreasDatas = new String[jsonAreas.length()];// 锟斤拷前锟叫碉拷锟斤拷锟斤拷锟斤拷
					for (int k = 0; k < jsonAreas.length(); k  )
					{
						String area = jsonAreas.getJSONObject(k).getString("s");// 锟斤拷锟斤拷锟斤拷锟斤拷
						mAreasDatas[k] = area;
					}
					mAreaDatasMap.put(city, mAreasDatas);
				}

				mCitisDatasMap.put(province, mCitiesDatas);
			}

		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		mJsonObj = null;
	}

	/**
	 * 锟斤拷assert锟侥硷拷锟斤拷锟叫讹拷取省锟斤拷锟斤拷锟絡son锟侥硷拷锟斤拷然锟斤拷转锟斤拷为json锟斤拷锟斤拷
	 */
	private void initJsonData()
	{
		try
		{
			StringBuffer sb = new StringBuffer();
			InputStream is = getAssets().open("city.json");
			int len = -1;
			byte[] buf = new byte[1024];
			while ((len = is.read(buf)) != -1)
			{
				sb.append(new String(buf, 0, len, "gbk"));
			}
			is.close();
			mJsonObj = new JSONObject(sb.toString());
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * change锟铰硷拷锟侥达拷锟斤拷
	 */
	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue)
	{
		if (wheel == mProvince)
		{
			mProvince.setViewAdapter(new ArrayWheelAdapter<String>(this, mProvinceDatas,newValue));
			
			updateCities(0);
			updateAreas(0);
			
			mCurrentAreaName="";
			
		} else if (wheel == mCity)
		{
//			updateCities(newValue);
//			updateAreas(0);
			int pCurrent = mProvince.getCurrentItem();
			mCurrentProviceName = mProvinceDatas[pCurrent];
			String[] cities = mCitisDatasMap.get(mCurrentProviceName);
			if (cities == null)
			{
				cities = new String[] { "" };
			}
			mCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities,newValue));
			int s = mCity.getCurrentItem();
			mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[s];
			String[] areas = mAreaDatasMap.get(mCurrentCityName);

			if (areas == null)
			{
				areas = new String[] { "" };
			}
			mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas,0));
			
		} else if (wheel == mArea)
		{
			int s = mCity.getCurrentItem();
			mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[s];
			String[] areas = mAreaDatasMap.get(mCurrentCityName);

			if (areas == null)
			{
				areas = new String[] { "" };
			}
			mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas,newValue));
			mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[newValue];
		}
	}

	public void showChoose(View view)
	{   
		if(mCurrentAreaName.equals("")){
			mCurrentAreaName=preferences.getString("sb", "");
		}
		Toast.makeText(this, mCurrentProviceName   mCurrentCityName   mCurrentAreaName, 1).show();
//		UIController.onBack(CitiesActivity.this, mCurrentProviceName, mCurrentCityName,mCurrentAreaName,
//				10);
	 
	}



}