7.Android开源项目WheelView的时间和地址联动选择对话框
类似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的时间和地址联动选择对话框的更多相关文章
- 开源项目DataTimePicker实现时间和日期的选择
这个开源项目是模仿Google官方的time选择器做的,是否漂亮.让我爱不释手,真心喜欢.很有幸和大家一起分享下,那么话不多说开始讲解. 开源项目地址:https://github.com/fl ...
- [转]Android开源项目第二篇——工具库篇
本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...
- 2015-2016最火的Android开源项目--github开源项目集锦(不看你就out了)
标签: Android开发开源项目最火Android项目github 2015-2016最火的Android开源项目 本文整理与集结了近期github上使用最广泛最火热与最流行的开源项目,想要充电与提 ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
- 59.Android开源项目及库 (转)
转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...
- GitHub上史上最全的Android开源项目分类汇总 (转)
GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...
- GitHub 优秀的 Android 开源项目(转)
今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...
- GitHub上史上最全的Android开源项目分类汇总
今天在看博客的时候,无意中发现了 @Trinea 在GitHub上的一个项目 Android开源项目分类汇总 ,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫 ...
- GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
随机推荐
- SQL Server 2012安装错误案例:Error while enabling Windows feature: NetFx3, Error Code: -2146498298
案例环境: 服务器环境 : Windows Server 2012 R2 Standard 数据库版本 : SQL Server 2012 SP1 案例介绍: 在Windows Ser ...
- Tornado框架简析
Tornado是一款轻量级的Web服务器,同时又是一个开发框架.采用单线程非阻塞I/O模型(epoll),主要是为了应对高并发 访问量而被开发出来,尤其适用于comet应用. Tornado服务器3大 ...
- SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在上一篇文章中已经分析了SQL SERVER中关于邮 ...
- 关于Redis数据过期策略
1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...
- Ubuntu搭建Note.Js 平台
1. 安装nodeJs和npm apt-get install nodejsapt-get install npm 2 .node有一个模块叫n,是专门用来管理node.js的版本的.首先安装n模块: ...
- 关于linx中man命令内容中第一行数字的含义
我们知道linux中man这玩意特别厉害,我们要查么个命令的使用方法.如man ls 出现如下内容 关于这写数字的含义如下表格
- 【转】【51CTO 网+】怎样做一款让用户来电的产品
[51CTO 网+]怎样做一款让用户来电的产品 据相关调查显示,目前全球移动用户平均每人安装应用约95个,每天使用的应用约35个.可见面对众多的移动应用,用户拥有非常大的选择空间.如果由于交互设计欠佳 ...
- linux init 启动顺序
redhat init大致启动过程 第一个运行的程序是/sbin/init,该文件会读取/etc/inittab文件,并依据此文件来进行初始化工作.比如在设定了运行等级 “:id:3:initdefa ...
- 【原】如何改变表单元素的外观(for Webkit and IE10)
表单元素在网页设计中使用的非常频繁,如文本输入框.单选框.复选框.选择列表.上传文件,它们在浏览器中的展现有自带的外观,为了在视觉上取得更好的产品体验,保持客户端的统一,通常产品经理会提出需要改变它的 ...
- Python+selenium自动化脚本编辑过程中遇到的问题和小技巧
应该也不算是问题和技巧,算是实践中学习到的Python,记录下,也不定时更新 1.通过截取url判断 实例: self.assertEqual(self.broswer.current_url[sel ...