Android 省市县 三级联动(android-wheel的使用)

4、布局文件,比较简单就3个WheelView分别代表省,市,县,还有一个按钮:

<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:background=“#000000”

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:textColor=“#ffffff”

android:textSize=“20sp” />

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:background=“@drawable/layout_bg”

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>

<Button

android:onClick=“showChoose”

android:layout_gravity=“right”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“确定”

/>

5、Activity的编写:注释相当详细,节不赘述了。

package com.example.wheel_province;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import kankan.wheel.widget.OnWheelChangedListener;

import kankan.wheel.widget.WheelView;

import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;

/**

  • @author zhy

*/

public class CitiesActivity extends Activity implements OnWheelChangedListener

{

/**

  • 把全国的省市区的信息以json的格式保存,解析完成后赋值为null

*/

private JSONObject mJsonObj;

/**

  • 省的WheelView控件

*/

private WheelView mProvince;

/**

  • 市的WheelView控件

*/

private WheelView mCity;

/**

  • 区的WheelView控件

*/

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 =“”;

@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);

initDatas();

mProvince.setViewAdapter(new ArrayWheelAdapter(this, mProvinceDatas));

// 添加change事件

mProvince.addChangingListener(this);

// 添加change事件

mCity.addChangingListener(this);

// 添加change事件

mArea.addChangingListener(this);

mProvince.setVisibleItems(5);

mCity.setVisibleItems(5);

mArea.setVisibleItems(5);

updateCities();

updateAreas();

}

/**

  • 根据当前的市,更新区WheelView的信息

*/

private void updateAreas()

{

int pCurrent = mCity.getCurrentItem();

mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];

String[] areas = mAreaDatasMap.get(mCurrentCityName);

if (areas == null)

{

areas = new String[] { “” };

}

mArea.setViewAdapter(new ArrayWheelAdapter(this, areas));

mArea.setCurrentItem(0);

}

/**

  • 根据当前的省,更新市WheelView的信息

*/

private void updateCities()

{

int pCurrent = mProvince.getCurrentItem();

mCurrentProviceName = mProvinceDatas[pCurrent];

String[] cities = mCitisDatasMap.get(mCurrentProviceName);

if (cities == null)

{

cities = new String[] { “” };

}

mCity.setViewAdapter(new ArrayWheelAdapter(this, cities));

mCity.setCurrentItem(0);

updateAreas();

}

/**

  • 解析整个Json对象,完成后释放Json对象的内存

*/

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)

{

总结

算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。

这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:




有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。

这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:

[外链图片转存中…(img-nmeobM0B-1714380970900)]
[外链图片转存中…(img-yu5b1MeY-1714380970901)]
[外链图片转存中…(img-QYkXNnWB-1714380970902)]
[外链图片转存中…(img-4jevHBla-1714380970904)]

有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值