本篇文章简单的实现了解析本地json文件,并添加到内存中,使用自定义控件,实现省市区三级联动的功能。
一、首先是效果图:
首先是CitiesActivity:
/**
* 省市区三级联动
* @author SHI
* 2016年6月1日 19:44:31
*/
public class CitiesActivity extends Activity implements OnWheelChangedListener
{
/**
* 把全国的省市区的信息以json的格式保存,解析完成后赋值为null
*/
private JSONObject mJsonObj;
private List<SelectPositionBean> listData_All;
private List<SelectPositionBean> listData_secound;
private List<SelectPositionBean> listData_third;
private SelectPositionBean selectCurrentFirstPositon;
private SelectPositionBean selectCurrentSecoundPositon;
private SelectPositionBean selectCurrentThirdPositon;
/**
* 省的WheelView控件
*/
private WheelView mProvince;
/**
* 市的WheelView控件
*/
private WheelView mCity;
/**
* 区的WheelView控件
*/
private WheelView mArea;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.citys);
initJsonData();
mProvince = (WheelView) findViewById(R.id.id_province);
mCity = (WheelView) findViewById(R.id.id_city);
mArea = (WheelView) findViewById(R.id.id_area);
parseJson();
mProvince.setViewAdapter(new MyWhellAdapter(this, listData_All));
// 添加change事件
mProvince.addChangingListener(this);
// 添加change事件
mCity.addChangingListener(this);
// 添加change事件
mArea.addChangingListener(this);
mProvince.setVisibleItems(7);
mCity.setVisibleItems(7);
mArea.setVisibleItems(7);
mProvince.setCurrentItem(0);
updateCities();
updateAreas();
}
private void parseJson(){
AssetManager am = getAssets();
Gson gson = new Gson();
try {
InputStream is = am.open("json_register_position.txt");
String str_json = inputStreamToString(is);
Log.i("str_json", str_json);
listData_All = gson.fromJson(str_json, new TypeToken<List<SelectPositionBean>>(){}.getType());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据当前的市,更新区WheelView的信息
*/
private void updateAreas()
{
int pCurrent = mCity.getCurrentItem();
selectCurrentSecoundPositon = listData_secound.get(pCurrent);
listData_third = selectCurrentSecoundPositon.getSubChinaCity();
mArea.setViewAdapter(new MyWhellAdapter(this, listData_third));
mArea.setCurrentItem(0);
if(listData_third != null && listData_third.size() != 0){
selectCurrentThirdPositon = listData_third.get(0);
}
}
/**
* 根据当前的省,更新市WheelView的信息
*/
private void updateCities()
{
int pCurrent = mProvince.getCurrentItem();
selectCurrentFirstPositon = listData_All.get(pCurrent);
listData_secound = selectCurrentFirstPositon.getSubChinaCity();
mCity.setViewAdapter(new MyWhellAdapter(this, listData_secound));
mCity.setCurrentItem(0);
updateAreas();
}
/**
* 从assert文件夹中读取省市区的json文件,然后转化为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)
{
updateCities();
} else if (wheel == mCity)
{
updateAreas();
} else if (wheel == mArea)
{
selectCurrentThirdPositon = listData_third.get(newValue);
}
}
public void showChoose(View view)
{
Toast.makeText(this, selectCurrentFirstPositon.getName() + selectCurrentSecoundPositon.getName() + selectCurrentThirdPositon.getName(), 1).show();
}
private class MyWhellAdapter extends BaseWheelTextAdapter<SelectPositionBean>{
public MyWhellAdapter(Context context, int itemResource,
int itemTextResource) {
super(context, itemResource, itemTextResource);
}
public MyWhellAdapter(Context context, int itemResource) {
super(context, itemResource);
}
public MyWhellAdapter(Context context, List<SelectPositionBean> listData) {
super(context, listData);
}
@Override
protected CharSequence getItemText(int index) {
return listData.get(index).getName();
}
}
/**
* 把流对象转换为字符串
* @param is
* @return
*/
public String inputStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
SelectPositionBean对象:
/**
* 地区对象
* @author SHI
* 2016年5月31日 16:32:25
*/
public class SelectPositionBean {
/**地区编号**/
private String Code;
/**地区名称**/
private String Name;
/**下属地区对象**/
private List<SelectPositionBean> SubChinaCity;
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public List<SelectPositionBean> getSubChinaCity() {
return SubChinaCity;
}
public void setSubChinaCity(List<SelectPositionBean> subChinaCity) {
SubChinaCity = subChinaCity;
}
}
布局文件citys.xml
<LinearLayout 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:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="请选择城市"
android:textSize="20sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<kankan.wheel.widget.WheelView
android:id="@+id/id_province"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
<kankan.wheel.widget.WheelView
android:id="@+id/id_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
<kankan.wheel.widget.WheelView
android:id="@+id/id_area"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</kankan.wheel.widget.WheelView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="showChoose"
android:text="确定" />
</LinearLayout>
其中用到了一个kankan.wheel.widget.WheelView自定义控件,因为代码有点琐碎,我就不粘贴代码了,直接给大家demo下载地址吧。