android sqlite绑定城市三级联(动态) 包含mysql数据源的sql语句

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值