源码下载:http://download.csdn.net/detail/qq_30716173/9462955
效果图如下所示,功能就是选择省份后自动显示该省份下面的城市,选择城市之后再选择区县,选择区县后可以点击确定,现在只是简单的弹出Toast显示选择的省市区。在buttongSureMethod()方法里面做确定按钮的操作,可以选择传递给其他页面等等。
数据库用的是网友写好的,所以做法就是把省市区的数据库放在res/raw下面,然后在程序中,把数据库用流拷贝到sd卡路径下,之后每次就查询数据库,获得省市区的信息就可以了。
public class DatabaseUtil { private Context mContext = null; private static final String DB_NAME = "china_province_city_zone.db"; private SQLiteDatabase db = null; private String filename; public DatabaseUtil(Context context) { mContext = context; } public void open() throws SQLException { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录 File file = new File(sdCardDir, DB_NAME); filename = sdCardDir + "/" + DB_NAME; if (!file.exists()) {//不存在,创建 try { file.createNewFile(); writeFromRaw(file); } catch (IOException e) { e.printStackTrace(); } } openDatabase(); } else { Toast.makeText(mContext, "没有SD卡哦!", Toast.LENGTH_SHORT).show(); } } public void openDatabase() { try { db = SQLiteDatabase.openOrCreateDatabase(filename, null); } catch (Exception e) { KyLog.object("openOrCreateDatabase异常"); } } private void writeFromRaw(File dbfile) { try { InputStream is = mContext.getResources().openRawResource( R.raw.china_province_city_zone); FileOutputStream fos = new FileOutputStream(dbfile); byte[] buffer = new byte[1024]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }之后是查询数据库的板块
/** * 查省份 * 传入空,查询所有省份,传入省份id,查询省份信息 * * @param id * @return */ public ArrayList<MyEntity> queryPro(String id) { openDatabase(); Cursor cursor; MyEntity mMyEntity; ArrayList<MyEntity> list = new ArrayList<MyEntity>(); // 若fileId为null或""则查询所有记录 if (id == null || id.equals("")) { cursor = db.rawQuery("select * from T_Province", null); } else { cursor = db.rawQuery("select * from T_Province" + " where ProSort=?", new String[]{id}); } while (cursor.moveToNext()) { mMyEntity = new MyEntity(); mMyEntity.setId(cursor.getString(cursor.getColumnIndex("ProSort"))); mMyEntity.setName(cursor.getString(cursor.getColumnIndex("ProName"))); mMyEntity.setParent_id("0"); list.add(mMyEntity); } cursor.close(); db.close(); return list; } /** * 查城市 * 传入省份id,查询该省下面的市 */ public ArrayList<MyEntity> queryCity(String id) { openDatabase(); Cursor cursor; MyEntity mMyEntity; ArrayList<MyEntity> list = new ArrayList<MyEntity>(); if (id != null && !id.equals("")) { cursor = db.rawQuery("select * from T_City" + " where ProID=?", new String[]{id}); // cursor = db.rawQuery("select * from T_City" + " where ProID="+id,null); while (cursor.moveToNext()) { mMyEntity = new MyEntity(); mMyEntity.setId(cursor.getString(cursor.getColumnIndex("CitySort"))); mMyEntity.setName(cursor.getString(cursor.getColumnIndex("CityName"))); mMyEntity.setParent_id(cursor.getString(cursor.getColumnIndex("ProID"))); list.add(mMyEntity); } cursor.close(); db.close(); return list; } return null; } /** * 查区县 * 输入市id,查询下面的区县 * * @param id * @return */ public ArrayList<MyEntity> queryCounty(String id) { openDatabase(); Cursor cursor; MyEntity mMyEntity; ArrayList<MyEntity> list = new ArrayList<MyEntity>(); // 若fileId为null或""则查询所有记录 cursor = db.rawQuery("select * from T_Zone" + " where CityID=?", new String[]{id}); if (cursor.moveToFirst() == false) { //为空的Cursor return null; } if (cursor != null && !cursor.equals("")) { while (cursor.moveToNext()) { mMyEntity = new MyEntity(); mMyEntity.setId(cursor.getString(cursor.getColumnIndex("ZoneID"))); mMyEntity.setName(cursor.getString(cursor.getColumnIndex("ZoneName"))); mMyEntity.setParent_id(cursor.getString(cursor.getColumnIndex("CityID"))); list.add(mMyEntity); } cursor.close(); db.close(); return list; } return null; } public void close() { if (db != null) db.close(); } }
AddressChoiceActivity做的就是点击选择地址button,弹出一个dialog,然后再dialog中三级联动让您选择地址。
public class AddressChoiceActivity extends Activity { private Dialog exitDialog; ListView mlistViewProvince; Button mBackButton; Button mSureButton; DatabaseUtil util; MyListAdapter mAdapter; List<MyEntity> list; TextView addressResult; TextView addressType; String strAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); util = new DatabaseUtil(AddressChoiceActivity.this); util.open(); } public void btnClick(View v) { showDialog(); } /** * 查询省 */ private void queryProvince() { addressType.setText("请选择省份"); list = util.queryPro(""); if (list != null) { mAdapter = new MyListAdapter(AddressChoiceActivity.this, list); mlistViewProvince.setAdapter(mAdapter); mlistViewProvince.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyEntity entity = (MyEntity) parent.getItemAtPosition(position); if (entity != null && !entity.equals("")) { queryCity(entity); } } }); mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (exitDialog != null) { exitDialog.cancel(); } } }); } } /** * 查询市 */ private void queryCity(MyEntity entity) { addressType.setText("请选择市级"); strAddress=entity.getName(); addressResult.setVisibility(View.VISIBLE); addressResult.setText(entity.getName()); list = util.queryCity(entity.getId()); if (list != null) { mAdapter.changeData(list); mlistViewProvince.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyEntity entity = (MyEntity) parent.getItemAtPosition(position); if (entity != null && !entity.equals("")) { strAddress = strAddress + "-" + entity.getName(); KyLog.object("区县============================"); KyLog.object(entity); queryCounty(entity); } } }); mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addressResult.setVisibility(View.GONE); queryProvince(); } }); } } /** * 查询区 */ private void queryCounty(final MyEntity cityEntity) { addressType.setText("请选择区县"); addressResult.setVisibility(View.VISIBLE); addressResult.setText(strAddress); list = util.queryCounty(cityEntity.getId()); if (list != null) { mAdapter.changeData(list); mlistViewProvince.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final MyEntity entity = (MyEntity) parent.getItemAtPosition(position); addressResult.setText(strAddress + "-" + entity.getName()); buttongSureMethod(); } }); mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSureButton.setVisibility(View.GONE); MyEntity entity = new MyEntity(); entity.setId(cityEntity.getParent_id()); entity.setName(strAddress.substring(0, strAddress.indexOf("-"))); entity.setParent_id("0"); queryCity(entity); } }); }else{ list=new ArrayList<>(); mAdapter.changeData(list); buttongSureMethod(); } } private void buttongSureMethod(){ mSureButton.setVisibility(View.VISIBLE); mBackButton.setVisibility(View.GONE); mSureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(AddressChoiceActivity.this, addressResult.getText().toString(), Toast.LENGTH_SHORT).show(); if (exitDialog != null) { exitDialog.cancel(); } } }); } private void showDialog() { exitDialog = new Dialog(AddressChoiceActivity.this); exitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); View view = getLayoutInflater().inflate(R.layout.address_layout, null); exitDialog.setContentView(view); init(view); exitDialog.show(); queryProvince(); addressResult = (TextView) view.findViewById(R.id.address_choice_result); } private void init(View view) { mlistViewProvince = (ListView) view.findViewById(R.id.address_listview); mBackButton = (Button) view.findViewById(R.id.address_backbutton); mSureButton = (Button) view.findViewById(R.id.address_firmbutton); addressType= (TextView) view.findViewById(R.id.name_pro_city_county); } @Override protected void onDestroy() { super.onDestroy(); util.close(); } }