实例介绍
【实例截图】
【核心代码】
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);
}
}
标签: 省市区的3级联动
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论