編輯:關於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(); Listlist = 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
最近做了手機虛擬按鈕開發,和華為榮耀的虛擬按鈕類似,效果如下(屏幕底下部分): 功能描述:手機開機,“虛擬按鈕”自動隱藏,當從手機下邊
和大家一起分享一下學習經驗,如何實現Android文件下載進度顯示功能,希望對廣大初學者有幫助。先上效果圖: 上方的藍色進度條,會根據文件下載量的百分比進行加載,中部的
Android應用開發的一項重要內容就是界面開發。對於用戶來說,不管APP包含的邏輯多麼復雜,功能多麼強大,如果沒有提供友好的圖形交互界面,將很難吸引最終用戶。作為一個程
本文實例講述了Android開發中MotionEvent坐標獲取方法。分享給大家供大家參考,具體如下:Android MotionEvent中getX()與getRawX