package com.icq.spinnercity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private DBManager dbm;
private SQLiteDatabase db;
private Spinner province_spinner = null;
private Spinner city_spinner = null;
private Spinner country_spinner = null;
private String provinceId = null;
private String cityId = null;
private String districtId = null;
private TextView tv_showselect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
province_spinner = (Spinner) findViewById(R.id.province_spinner);
city_spinner = (Spinner) findViewById(R.id.city_spinner);
country_spinner = (Spinner) findViewById(R.id.country_spinner);
tv_showselect = (TextView) findViewById(R.id.tv_showselect);
province_spinner.setPrompt("请选择省份");
city_spinner.setPrompt("请选择城市");
country_spinner.setPrompt("请选择地区");
initProvince();
}
public void initProvince() {
dbm = new DBManager(this);
dbm.openDatabase();
db = dbm.getDatabase();
List<SpinnerItem> list = new ArrayList<SpinnerItem>();
try {
String sql = "select * from fs_province";
Cursor cursor = db.rawQuery(sql, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String code = cursor.getString(cursor
.getColumnIndex("ProvinceID"));
String name = cursor.getString(cursor
.getColumnIndex("ProvinceName"));
SpinnerItem myListItem = new SpinnerItem();
myListItem.setName(name);
myListItem.setPcode(code);
list.add(myListItem);
cursor.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
dbm.closeDatabase();
db.close();
SpinnerAdapter myAdapter = new SpinnerAdapter(this, list);
province_spinner.setAdapter(myAdapter);
province_spinner
.setOnItemSelectedListener(new ProvinceOnSelectedListener());
}
public void initCity(String pcode) {
dbm = new DBManager(this);
dbm.openDatabase();
db = dbm.getDatabase();
List<SpinnerItem> list = new ArrayList<SpinnerItem>();
try {
String sql = "select * from fs_city where ProvinceID='" + pcode
+ "'";
Cursor cursor = db.rawQuery(sql, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String code = cursor.getString(cursor.getColumnIndex("CityID"));
String name = cursor.getString(cursor
.getColumnIndex("CityName"));
SpinnerItem myListItem = new SpinnerItem();
myListItem.setName(name);
myListItem.setPcode(code);
list.add(myListItem);
cursor.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
dbm.closeDatabase();
db.close();
SpinnerAdapter myAdapter = new SpinnerAdapter(this, list);
city_spinner.setAdapter(myAdapter);
city_spinner
.setOnItemSelectedListener(new CityOnSelectedListener());
}
public void initCountry(String pcode) {
dbm = new DBManager(this);
dbm.openDatabase();
db = dbm.getDatabase();
List<SpinnerItem> list = new ArrayList<SpinnerItem>();
try {
String sql = "select * from fs_district where CityID='" + pcode
+ "'";
Cursor cursor = db.rawQuery(sql, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String code = cursor.getString(cursor
.getColumnIndex("DistrictID"));
String name = cursor.getString(cursor
.getColumnIndex("DistrictName"));
SpinnerItem myListItem = new SpinnerItem();
myListItem.setName(name);
myListItem.setPcode(code);
list.add(myListItem);
cursor.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
dbm.closeDatabase();
db.close();
SpinnerAdapter myAdapter = new SpinnerAdapter(this, list);
country_spinner.setAdapter(myAdapter);
country_spinner
.setOnItemSelectedListener(new DistrictOnSelectedListener());
}
class ProvinceOnSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
provinceId = ((SpinnerItem) adapterView.getItemAtPosition(position))
.getPcode();
String pcode = ((SpinnerItem) adapterView
.getItemAtPosition(position)).getPcode();
initCity(pcode);
initCountry(pcode);
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
class CityOnSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
cityId = ((SpinnerItem) adapterView.getItemAtPosition(position))
.getPcode();
String pcode = ((SpinnerItem) adapterView
.getItemAtPosition(position)).getPcode();
initCountry(pcode);
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
class DistrictOnSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
districtId = ((SpinnerItem) adapterView.getItemAtPosition(position))
.getPcode();
// 这里我是现实所选择的id,您可以现实汉字
tv_showselect.setText("省份Id=" + provinceId + ",城市Id=" + cityId
+ ",地区Id:" + districtId);
//其他请求的操作
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
}
无图无真相,上图如下:
资源下载地址:http://download.csdn.net/detail/shi508181017/6908631
有什么不对请大家多多指教,有什么不对会问题可以联系我QQ:508181017