Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android sqlite綁定城市三級聯(動態) 包含mysql數據源的sql語句

android sqlite綁定城市三級聯(動態) 包含mysql數據源的sql語句

編輯:關於Android編程

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 list = new ArrayList();
		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 list = new ArrayList();

		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 list = new ArrayList();

		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




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved