Android省市区3级联动的地址列表的实现

我们在上一篇中介绍了如何获取省市区的数据,现在我们来介绍使用spinner控件来实现省市区3级联动的例子。

下面的代码是我在开发项目时实现的。

首先我们展示下布局文件中的3个spinner

<LinearLayout android:layout_height="40dip"
android:layout_width="fill_parent"
android:id="@+id/LinearLayoutArea" android:orientation="horizontal">
<Spinner android:id="@+id/spprovince" android:layout_width="100dip"
android:layout_height="wrap_content">
</Spinner>
<Spinner android:id="@+id/spcity" android:layout_width="100dip"
android:layout_height="wrap_content">
</Spinner>
<Spinner android:id="@+id/sparea" android:layout_width="100dip"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>

接下来是对这些spinner控件赋值绑定事件的代码:

// 从数据库中读取省市区的数据
// 初始化省下拉框
private void InitialProvince() {
try {
Map<Integer, List> data = AddressUtil.getProvince(file);
provinceList = data.get(1);
provinceData = data.get(0);
ArrayAdapter provinceAdapter = new ArrayAdapter(mContext,
android.R.layout.simple_spinner_item, provinceList);
provinceAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 样式
spprovince.setAdapter(provinceAdapter);
spprovince.setOnItemSelectedListener(itemSelectedListener);
} catch (Exception e) {
Log.d("WineStock", "InitialProvince:" + e.getMessage());
}
}

String selectedProvince = "";// 下拉框中选择的省
// 省下拉框事件
private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
try {
Spinner spProvince = (Spinner) parent;
selectedProvince = (String) spProvince
.getItemAtPosition(position);
ArrayAdapter cityAdapter = null;
Map<String, Integer> data = (Map) provinceData.get(position);// 根据所选择的得到对应的省份id
// 然后得到城市
int pid = data.get(selectedProvince);// 得到省份的id
Map<Integer, List> citymap = AddressUtil
.getCityByPid(pid, file);
cityList = citymap.get(1);
cityData = citymap.get(0);
cityAdapter = new ArrayAdapter(mContext,
android.R.layout.simple_spinner_item, cityList);
cityAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spcity.setAdapter(cityAdapter);
spcity.setOnItemSelectedListener(citySelectedListener);
} catch (Exception e) {
Log.d("WineStock", "Select Province error:" + e.getMessage());
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};

String selectedCity = "";// 下拉框中选择的市
// 市下拉框事件
private OnItemSelectedListener citySelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
try {
Spinner spCity = (Spinner) parent;
selectedCity = (String) spCity.getItemAtPosition(position);
ArrayAdapter areaAdapter = null;
Map<String, Integer> data = (Map) cityData.get(position);// 根据所选择的得到对应的省份id
// 然后得到城市
int pid = data.get(selectedCity);// 得到城市的id
List<String> listArea = AddressUtil.getAreaByPid(pid, file);
areaAdapter = new ArrayAdapter(mContext,
android.R.layout.simple_spinner_item, listArea);
areaAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sparea.setAdapter(areaAdapter);
sparea.setOnItemSelectedListener(areaSelectedListener);
} catch (Exception e) {
Log.d("WineStock", "Select City error:" + e.getMessage());
}

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};

String selectedArea = "";// 下拉框中选择的区
// 区下拉框事件
private OnItemSelectedListener areaSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
try {
Spinner spArea = (Spinner) parent;
selectedArea = (String) spArea.getItemAtPosition(position);
if (selectedProvince != "" && selectedCity != ""
&& selectedArea != "") {
String strAdd = selectedProvince + selectedCity
+ selectedArea;
etaddress.setText(strAdd);
}
} catch (Exception e) {
Log.d("WineStock", "Select Area error:" + e.getMessage());
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};





你可能感兴趣的:(android)