7.Android开源项目WheelView的时间和地址联动选择对话框

时间:2021-12-29 14:36:22

类似WheelView的时间和地址联动选择对话框在现在App经常看到,今天小结下。

主布局界面:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" > <TextView
android:id="@+id/tv_birth"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:layout_marginTop="50dip"
android:background="@drawable/btn_blue_selector"
android:gravity="center"
android:text="选择时间" />
<TextView
android:id="@+id/tv_address"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:layout_marginTop="24dip"
android:background="@drawable/btn_blue_selector"
android:gravity="center"
android:text="选择地点"/> </LinearLayout>

选择时间布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_myinfo_changebirth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" > <LinearLayout
android:id="@+id/ly_myinfo_changebirth_child"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginLeft="27dip"
android:layout_marginRight="27dip"
android:background="@drawable/bg_dialog_selector"
android:orientation="vertical" > <TextView
android:id="@+id/tv_share_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dip"
android:layout_marginLeft="18dip"
android:layout_marginTop="18dip"
android:gravity="center_vertical"
android:text="选择日期"
android:textColor="#000000"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dip"
android:orientation="horizontal"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="center_vertical">
<com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
android:id="@+id/wv_birth_year"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
/>
<com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
android:id="@+id/wv_birth_month"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
/>
<com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
android:id="@+id/wv_birth_day"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_marginBottom="21dip"> <TextView
android:id="@+id/btn_myinfo_sure"
android:layout_width="77dip"
android:layout_height="33dip"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip"
android:text="确认"
android:gravity="center"
android:textSize="16sp" /> <TextView
android:id="@+id/btn_myinfo_cancel"
android:layout_width="77dip"
android:layout_height="33dip"
android:layout_toLeftOf="@+id/btn_myinfo_sure"
android:text="取消"
android:gravity="center"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout> </LinearLayout>

选择地点布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ly_myinfo_changeaddress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" > <LinearLayout
android:id="@+id/ly_myinfo_changeaddress_child"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginLeft="27dip"
android:layout_marginRight="27dip"
android:background="@drawable/bg_dialog_selector"
android:orientation="vertical" > <TextView
android:id="@+id/tv_share_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dip"
android:layout_marginLeft="18dip"
android:layout_marginTop="18dip"
android:gravity="center_vertical"
android:text="选择地区"
android:textColor="#000000"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dip"
android:orientation="horizontal"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="center_vertical">
<com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
android:id="@+id/wv_address_province"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
/>
<com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
android:id="@+id/wv_address_city"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_marginBottom="21dip" > <TextView
android:id="@+id/btn_myinfo_sure"
android:layout_width="77dip"
android:layout_height="33dip"
android:text="确认"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip"
android:textSize="16sp" /> <TextView
android:id="@+id/btn_myinfo_cancel"
android:layout_width="77dip"
android:layout_height="33dip"
android:layout_toLeftOf="@+id/btn_myinfo_sure"
android:gravity="center"
android:text="取消"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout> </LinearLayout>

时间对话框代码:

 package com.example.riger.wheelviewdemo;

 import java.util.ArrayList;
import java.util.Calendar; import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.riger.wheelviewdemo.wheel.widget.adapters.AbstractWheelTextAdapter;
import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelChangedListener;
import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelScrollListener;
import com.example.riger.wheelviewdemo.wheel.widget.views.WheelView; /**
* 日期选择对话框
*
* @author ywl
*
*/
public class ChangeBirthDialog extends Dialog implements android.view.View.OnClickListener { private Context context;
private WheelView wvYear;
private WheelView wvMonth;
private WheelView wvDay; private View vChangeBirth;
private View vChangeBirthChild;
private TextView btnSure;
private TextView btnCancel; private ArrayList<String> arry_years = new ArrayList<String>();
private ArrayList<String> arry_months = new ArrayList<String>();
private ArrayList<String> arry_days = new ArrayList<String>();
private CalendarTextAdapter mYearAdapter;
private CalendarTextAdapter mMonthAdapter;
private CalendarTextAdapter mDaydapter; private int month;
private int day; private int currentYear = getYear();
private int currentMonth = 1;
private int currentDay = 1; private int maxTextSize = 24;
private int minTextSize = 14; private boolean issetdata = false; private String selectYear;
private String selectMonth;
private String selectDay; private OnBirthListener onBirthListener; public ChangeBirthDialog(Context context) {
super(context, R.style.ShareDialog);
this.context = context;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_myinfo_changebirth);
wvYear = (WheelView) findViewById(R.id.wv_birth_year);
wvMonth = (WheelView) findViewById(R.id.wv_birth_month);
wvDay = (WheelView) findViewById(R.id.wv_birth_day); vChangeBirth = findViewById(R.id.ly_myinfo_changebirth);
vChangeBirthChild = findViewById(R.id.ly_myinfo_changebirth_child);
btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel); vChangeBirth.setOnClickListener(this);
vChangeBirthChild.setOnClickListener(this);
btnSure.setOnClickListener(this);
btnCancel.setOnClickListener(this); if (!issetdata) {
initData();
}
initYears();
mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize);
wvYear.setVisibleItems(5);
wvYear.setViewAdapter(mYearAdapter);
wvYear.setCurrentItem(setYear(currentYear)); initMonths(month);
mMonthAdapter = new CalendarTextAdapter(context, arry_months, setMonth(currentMonth), maxTextSize, minTextSize);
wvMonth.setVisibleItems(5);
wvMonth.setViewAdapter(mMonthAdapter);
wvMonth.setCurrentItem(setMonth(currentMonth)); initDays(day);
mDaydapter = new CalendarTextAdapter(context, arry_days, currentDay - 1, maxTextSize, minTextSize);
wvDay.setVisibleItems(5);
wvDay.setViewAdapter(mDaydapter);
wvDay.setCurrentItem(currentDay - 1); wvYear.addChangingListener(new OnWheelChangedListener() { @Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
selectYear = currentText;
setTextviewSize(currentText, mYearAdapter);
currentYear = Integer.parseInt(currentText);
setYear(currentYear);
initMonths(month);
mMonthAdapter = new CalendarTextAdapter(context, arry_months, 0, maxTextSize, minTextSize);
wvMonth.setVisibleItems(5);
wvMonth.setViewAdapter(mMonthAdapter);
wvMonth.setCurrentItem(0);
}
}); wvYear.addScrollingListener(new OnWheelScrollListener() { @Override
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub } @Override
public void onScrollingFinished(WheelView wheel) {
// TODO Auto-generated method stub
String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, mYearAdapter);
}
}); wvMonth.addChangingListener(new OnWheelChangedListener() { @Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
selectMonth = currentText;
setTextviewSize(currentText, mMonthAdapter);
setMonth(Integer.parseInt(currentText));
initDays(day);
mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize);
wvDay.setVisibleItems(5);
wvDay.setViewAdapter(mDaydapter);
wvDay.setCurrentItem(0);
}
}); wvMonth.addScrollingListener(new OnWheelScrollListener() { @Override
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub } @Override
public void onScrollingFinished(WheelView wheel) {
// TODO Auto-generated method stub
String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, mMonthAdapter);
}
}); wvDay.addChangingListener(new OnWheelChangedListener() { @Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, mDaydapter);
selectDay = currentText;
}
}); wvDay.addScrollingListener(new OnWheelScrollListener() { @Override
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub } @Override
public void onScrollingFinished(WheelView wheel) {
// TODO Auto-generated method stub
String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, mDaydapter);
}
}); } public void initYears() {
for (int i = getYear(); i > 1950; i--) {
arry_years.add(i + "");
}
} public void initMonths(int months) {
arry_months.clear();
for (int i = 1; i <= months; i++) {
arry_months.add(i + "");
}
} public void initDays(int days) {
arry_days.clear();
for (int i = 1; i <= days; i++) {
arry_days.add(i + "");
}
} private class CalendarTextAdapter extends AbstractWheelTextAdapter {
ArrayList<String> list; protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
this.list = list;
setItemTextResource(R.id.tempValue);
} @Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
return view;
} @Override
public int getItemsCount() {
return list.size();
} @Override
protected CharSequence getItemText(int index) {
return list.get(index) + "";
}
} public void setBirthdayListener(OnBirthListener onBirthListener) {
this.onBirthListener = onBirthListener;
} @Override
public void onClick(View v) { if (v == btnSure) {
if (onBirthListener != null) {
onBirthListener.onClick(selectYear, selectMonth, selectDay);
}
} else if (v == btnSure) { } else if (v == vChangeBirthChild) {
return;
} else {
dismiss();
}
dismiss(); } public interface OnBirthListener {
public void onClick(String year, String month, String day);
} /**
* 设置字体大小
*
* @param curriteItemText
* @param adapter
*/
public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
ArrayList<View> arrayList = adapter.getTestViews();
int size = arrayList.size();
String currentText;
for (int i = 0; i < size; i++) {
TextView textvew = (TextView) arrayList.get(i);
currentText = textvew.getText().toString();
if (curriteItemText.equals(currentText)) {
textvew.setTextSize(maxTextSize);
} else {
textvew.setTextSize(minTextSize);
}
}
} public int getYear() {
Calendar c = Calendar.getInstance();
return c.get(Calendar.YEAR);
} public int getMonth() {
Calendar c = Calendar.getInstance();
return c.get(Calendar.MONTH) + 1;
} public int getDay() {
Calendar c = Calendar.getInstance();
return c.get(Calendar.DATE);
} public void initData() {
setDate(getYear(), getMonth(), getDay());
this.currentDay = 1;
this.currentMonth = 1;
} /**
* 设置年月日
*
* @param year
* @param month
* @param day
*/
public void setDate(int year, int month, int day) {
selectYear = year + "";
selectMonth = month + "";
selectDay = day + "";
issetdata = true;
this.currentYear = year;
this.currentMonth = month;
this.currentDay = day;
if (year == getYear()) {
this.month = getMonth();
} else {
this.month = 12;
}
calDays(year, month);
} /**
* 设置年份
*
* @param year
*/
public int setYear(int year) {
int yearIndex = 0;
if (year != getYear()) {
this.month = 12;
} else {
this.month = getMonth();
}
for (int i = getYear(); i > 1950; i--) {
if (i == year) {
return yearIndex;
}
yearIndex++;
}
return yearIndex;
} /**
* 设置月份
*
* @param year
* @param month
* @return
*/
public int setMonth(int month) {
int monthIndex = 0;
calDays(currentYear, month);
for (int i = 1; i < this.month; i++) {
if (month == i) {
return monthIndex;
} else {
monthIndex++;
}
}
return monthIndex;
} /**
* 计算每月多少天
*
* @param month
* @param leayyear
*/
public void calDays(int year, int month) {
boolean leayyear = false;
if (year % 4 == 0 && year % 100 != 0) {
leayyear = true;
} else {
leayyear = false;
}
for (int i = 1; i <= 12; i++) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
this.day = 31;
break;
case 2:
if (leayyear) {
this.day = 29;
} else {
this.day = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
this.day = 30;
break;
}
}
if (year == getYear() && month == getMonth()) {
this.day = getDay();
}
}
}

选择地点对话框代码:

 package com.example.riger.wheelviewdemo;

 import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.riger.wheelviewdemo.wheel.widget.adapters.AbstractWheelTextAdapter;
import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelChangedListener;
import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelScrollListener;
import com.example.riger.wheelviewdemo.wheel.widget.views.WheelView; /**
* 更改封面对话框
*
* @author ywl
*
*/
public class ChangeAddressDialog extends Dialog implements android.view.View.OnClickListener { private WheelView wvProvince;
private WheelView wvCitys;
private View lyChangeAddress;
private View lyChangeAddressChild;
private TextView btnSure;
private TextView btnCancel; private Context context;
private JSONObject mJsonObj;
private String[] mProvinceDatas;
private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>(); private ArrayList<String> arrProvinces = new ArrayList<String>();
private ArrayList<String> arrCitys = new ArrayList<String>();
private AddressTextAdapter provinceAdapter;
private AddressTextAdapter cityAdapter; private String strProvince = "湖南";
private String strCity = "长沙";
private OnAddressCListener onAddressCListener; private int maxsize = 24;
private int minsize = 14; public ChangeAddressDialog(Context context) {
super(context, R.style.ShareDialog);
this.context = context;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_myinfo_changeaddress); wvProvince = (WheelView) findViewById(R.id.wv_address_province);
wvCitys = (WheelView) findViewById(R.id.wv_address_city);
lyChangeAddress = findViewById(R.id.ly_myinfo_changeaddress);
lyChangeAddressChild = findViewById(R.id.ly_myinfo_changeaddress_child);
btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel); lyChangeAddress.setOnClickListener(this);
lyChangeAddressChild.setOnClickListener(this);
btnSure.setOnClickListener(this);
btnCancel.setOnClickListener(this); initJsonData();
initDatas();
initProvinces();
provinceAdapter = new AddressTextAdapter(context, arrProvinces, getProvinceItem(strProvince), maxsize, minsize);
wvProvince.setVisibleItems(5);
wvProvince.setViewAdapter(provinceAdapter);
wvProvince.setCurrentItem(getProvinceItem(strProvince)); initCitys(mCitisDatasMap.get(strProvince));
cityAdapter = new AddressTextAdapter(context, arrCitys, getCityItem(strCity), maxsize, minsize);
wvCitys.setVisibleItems(5);
wvCitys.setViewAdapter(cityAdapter);
wvCitys.setCurrentItem(getCityItem(strCity)); wvProvince.addChangingListener(new OnWheelChangedListener() { @Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
strProvince = currentText;
setTextviewSize(currentText, provinceAdapter);
String[] citys = mCitisDatasMap.get(currentText);
initCitys(citys);
cityAdapter = new AddressTextAdapter(context, arrCitys, 0, maxsize, minsize);
wvCitys.setVisibleItems(5);
wvCitys.setViewAdapter(cityAdapter);
wvCitys.setCurrentItem(0);
}
}); wvProvince.addScrollingListener(new OnWheelScrollListener() { @Override
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub } @Override
public void onScrollingFinished(WheelView wheel) {
// TODO Auto-generated method stub
String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, provinceAdapter);
}
}); wvCitys.addChangingListener(new OnWheelChangedListener() { @Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
String currentText = (String) cityAdapter.getItemText(wheel.getCurrentItem());
strCity = currentText;
setTextviewSize(currentText, cityAdapter);
}
}); wvCitys.addScrollingListener(new OnWheelScrollListener() { @Override
public void onScrollingStarted(WheelView wheel) {
// TODO Auto-generated method stub } @Override
public void onScrollingFinished(WheelView wheel) {
// TODO Auto-generated method stub
String currentText = (String) cityAdapter.getItemText(wheel.getCurrentItem());
setTextviewSize(currentText, cityAdapter);
}
});
} private class AddressTextAdapter extends AbstractWheelTextAdapter {
ArrayList<String> list; protected AddressTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
this.list = list;
setItemTextResource(R.id.tempValue);
} @Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
return view;
} @Override
public int getItemsCount() {
return list.size();
} @Override
protected CharSequence getItemText(int index) {
return list.get(index) + "";
}
} /**
* 设置字体大小
*
* @param curriteItemText
* @param adapter
*/
public void setTextviewSize(String curriteItemText, AddressTextAdapter adapter) {
ArrayList<View> arrayList = adapter.getTestViews();
int size = arrayList.size();
String currentText;
for (int i = 0; i < size; i++) {
TextView textvew = (TextView) arrayList.get(i);
currentText = textvew.getText().toString();
if (curriteItemText.equals(currentText)) {
textvew.setTextSize(24);
} else {
textvew.setTextSize(14);
}
}
} public void setAddresskListener(OnAddressCListener onAddressCListener) {
this.onAddressCListener = onAddressCListener;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnSure) {
if (onAddressCListener != null) {
onAddressCListener.onClick(strProvince, strCity);
}
} else if (v == btnCancel) { } else if (v == lyChangeAddressChild) {
return;
} else {
dismiss();
}
dismiss();
} /**
* 回调接口
*
* @author Administrator
*
*/
public interface OnAddressCListener {
public void onClick(String province, String city);
} /**
* 从文件中读取地址数据
*/
private void initJsonData() {
try {
StringBuffer sb = new StringBuffer();
//InputStream is = context.getAssets().open("city.json");
InputStream is = context.getClass().getClassLoader().getResourceAsStream("assets/" + "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();
}
} /**
* 解析数据
*/
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);
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;
}
}
mCitisDatasMap.put(province, mCitiesDatas);
} } catch (JSONException e) {
e.printStackTrace();
}
mJsonObj = null;
} /**
* 初始化省会
*/
public void initProvinces() {
int length = mProvinceDatas.length;
for (int i = 0; i < length; i++) {
arrProvinces.add(mProvinceDatas[i]);
}
} /**
* 根据省会,生成该省会的所有城市
*
* @param citys
*/
public void initCitys(String[] citys) {
if (citys != null) {
arrCitys.clear();
int length = citys.length;
for (int i = 0; i < length; i++) {
arrCitys.add(citys[i]);
}
} else {
String[] city = mCitisDatasMap.get("湖南");
arrCitys.clear();
int length = city.length;
for (int i = 0; i < length; i++) {
arrCitys.add(city[i]);
}
}
if (arrCitys != null && arrCitys.size() > 0
&& !arrCitys.contains(strCity)) {
strCity = arrCitys.get(0);
}
} /**
* 初始化地点
*
* @param province
* @param city
*/
public void setAddress(String province, String city) {
if (province != null && province.length() > 0) {
this.strProvince = province;
}
if (city != null && city.length() > 0) {
this.strCity = city;
}
} /**
* 返回省会索引,没有就返回默认“湖南”
*
* @param province
* @return
*/
public int getProvinceItem(String province) {
int size = arrProvinces.size();
int provinceIndex = 0;
boolean noprovince = true;
for (int i = 0; i < size; i++) {
if (province.equals(arrProvinces.get(i))) {
noprovince = false;
return provinceIndex;
} else {
provinceIndex++;
}
}
if (noprovince) {
strProvince = "湖南";
return 17;
}
return provinceIndex;
} /**
* 得到城市索引,没有返回默认“长沙”
*
* @param city
* @return
*/
public int getCityItem(String city) {
int size = arrCitys.size();
int cityIndex = 0;
boolean nocity = true;
for (int i = 0; i < size; i++) {
System.out.println(arrCitys.get(i));
if (city.equals(arrCitys.get(i))) {
nocity = false;
return cityIndex;
} else {
cityIndex++;
}
}
if (nocity) {
strCity = "长沙";
return 0;
}
return cityIndex;
} }

运行效果:

7.Android开源项目WheelView的时间和地址联动选择对话框  7.Android开源项目WheelView的时间和地址联动选择对话框  7.Android开源项目WheelView的时间和地址联动选择对话框    7.Android开源项目WheelView的时间和地址联动选择对话框  7.Android开源项目WheelView的时间和地址联动选择对话框