借助WheelView 以Dialog形式显示城市三级关联选择器
BaseDialog.java
方法转载:http://blog.csdn.net/wulianghuan/article/details/41549189
dialog_set_city.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="wrap_content"
android:background="@color/white"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_theme_title_bg"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="所在地区"
android:textColor="@color/white"
android:textSize="20sp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<cn.sc.single.widget.WheelView
android:id="@+id/id_province"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white">
</cn.sc.single.widget.WheelView>
<cn.sc.single.widget.WheelView
android:id="@+id/id_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white">
</cn.sc.single.widget.WheelView>
<cn.sc.single.widget.WheelView
android:id="@+id/id_district"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white">
</cn.sc.single.widget.WheelView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_gary"
android:orientation="horizontal">
<cn.sc.single.view.AutoTextView
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_weight="1"
android:background="@color/darkorange"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textColor="@color/white"/>
<cn.sc.single.view.AutoTextView
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_weight="1"
android:background="@color/app_theme_title_bg"
android:gravity="center"
android:padding="10dp"
android:text="确定"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
自定义SetAddressDialog.java
package cn.sc.single.dialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import cn.sc.single.R;
import cn.sc.single.bean.CityModel;
import cn.sc.single.bean.DistrictModel;
import cn.sc.single.bean.ProvinceModel;
import cn.sc.single.utils.XmlParserHandler;
import cn.sc.single.widget.OnWheelChangedListener;
import cn.sc.single.widget.WheelView;
import cn.sc.single.widget.adapters.ArrayWheelAdapter;
public class SetAddressDialog extends BaseDialog implements OnWheelChangedListener {
private static int default_width = -1;
private static int default_height = -2;
private TextView tvCancel;
private TextView tvSure;
private SureListener sureListener;
private WheelView mViewProvince;
private WheelView mViewCity;
private WheelView mViewDistrict;
protected String[] mProvinceDatas;
protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();
protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();
protected String mCurrentProviceName;
protected String mCurrentCityName;
protected String mCurrentDistrictName = "";
protected String mCurrentZipCode = "";
public SetAddressDialog(Context context) {
super(context, default_width, default_height, R.layout.dialog_set_city, R.style.DialogTheme, Gravity.BOTTOM);
this.setCancelable(true);
init();
setUpListener();
setUpData();
}
protected void initProvinceDatas() {
List<ProvinceModel> provinceList = null;
AssetManager asset = context.getAssets();
try {
InputStream input = asset.open("province_data.xml");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XmlParserHandler handler = new XmlParserHandler();
parser.parse(input, handler);
input.close();
provinceList = handler.getDataList();
if (provinceList != null && !provinceList.isEmpty()) {
mCurrentProviceName = provinceList.get(0).getName();
List<CityModel> cityList = provinceList.get(0).getCityList();
if (cityList != null && !cityList.isEmpty()) {
mCurrentCityName = cityList.get(0).getName();
List<DistrictModel> districtList = cityList.get(0).getDistrictList();
mCurrentDistrictName = districtList.get(0).getName();
mCurrentZipCode = districtList.get(0).getZipcode();
}
}
mProvinceDatas = new String[provinceList.size()];
for (int i = 0; i < provinceList.size(); i++) {
mProvinceDatas[i] = provinceList.get(i).getName();
List<CityModel> cityList = provinceList.get(i).getCityList();
String[] cityNames = new String[cityList.size()];
for (int j = 0; j < cityList.size(); j++) {
cityNames[j] = cityList.get(j).getName();
List<DistrictModel> districtList = cityList.get(j).getDistrictList();
String[] distrinctNameArray = new String[districtList.size()];
DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
for (int k = 0; k < districtList.size(); k++) {
DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
distrinctArray[k] = districtModel;
distrinctNameArray[k] = districtModel.getName();
}
mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
}
mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
}
}
private void setUpData() {
initProvinceDatas();
mViewProvince.setBackgroundColor(context.getResources().getColor(R.color.white));
mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(context, mProvinceDatas));
mViewProvince.setVisibleItems(7);
mViewCity.setVisibleItems(7);
mViewDistrict.setVisibleItems(7);
updateCities();
updateAreas();
}
private void setUpListener() {
mViewProvince.addChangingListener(this);
mViewCity.addChangingListener(this);
mViewDistrict.addChangingListener(this);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
private void init() {
tvSure = (TextView) findViewById(R.id.btn_confirm);
tvCancel = (TextView) findViewById(R.id.btn_cancel);
mViewProvince = (WheelView) findViewById(R.id.id_province);
mViewCity = (WheelView) findViewById(R.id.id_city);
mViewDistrict = (WheelView) findViewById(R.id.id_district);
tvSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sureListener != null) {
sureListener.onSure(mCurrentProviceName + mCurrentCityName + mCurrentDistrictName);
}
SetAddressDialog.this.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SetAddressDialog.this.dismiss();
}
});
}
public void setSureListener(SureListener sureListener) {
this.sureListener = sureListener;
}
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (wheel == mViewProvince) {
updateCities();
} else if (wheel == mViewCity) {
updateAreas();
} else if (wheel == mViewDistrict) {
mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
}
}
public interface SureListener {
public void onSure(String address);
}
private void updateAreas() {
int pCurrent = mViewCity.getCurrentItem();
mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
String[] areas = mDistrictDatasMap.get(mCurrentCityName);
if (areas == null) {
areas = new String[]{""};
}
mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(context, areas));
mViewDistrict.setCurrentItem(0);
}
private void updateCities() {
int pCurrent = mViewProvince.getCurrentItem();
mCurrentProviceName = mProvinceDatas[pCurrent];
String[] cities = mCitisDatasMap.get(mCurrentProviceName);
if (cities == null) {
cities = new String[]{""};
}
mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(context, cities));
mViewCity.setCurrentItem(0);
updateAreas();
}
}
BaseDialog.java
package cn.sc.single.dialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;
import cn.sc.single.common.DataMgr;
public class BaseDialog extends Dialog {
protected Context context;
protected BaseDialog(Context context) {
super(context);
}
protected BaseDialog(Context context, int width, int height, int layout, int style, int gravity) {
super(context, style);
this.context = context;
setContentView(layout);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
if (width == -1) {
params.width = WindowManager.LayoutParams.MATCH_PARENT;
} else if (width == -2) {
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
} else {
params.width = (int) (width * DataMgr.screenDensity);
}
if (height == -1) {
params.height = WindowManager.LayoutParams.MATCH_PARENT;
} else if (height == -2) {
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
} else {
params.height = (int) (height * DataMgr.screenDensity);
}
params.gravity = gravity;
window.setAttributes(params);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
}