Android 實現省份城市的選擇,並獲取城市編號
該程式主要使用 中央氣象局 省份 城市資料庫為基礎 進行讀取
城市資料庫下載 http://download.csdn.net/download/xianqiang1/3896880 感謝該兄弟的分享
下載的資料庫 db_weather.db 放到sdcard/weather 目錄下面 方便後續操作
為了更好的瞭解資料庫,使用 SQLite Database Browser 可以開啟資料庫 檢視資料 和表等資訊,如下
瞭解了表的構成可以實現操作了
androidManifest.xml
配置檔案宣告 新增操作sdcard 許可權
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cityselection"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <!-- sdcard操作允許 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".City_SelectionActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cityselection"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <!-- sdcard操作允許 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".City_SelectionActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
佈局檔案main.xml
主要使用兩個 spinner 分別實現城市 省份的選擇
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:text="省份/直轄市"
- android:textSize="20dp"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Spinner
- android:id="@+id/provinces"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:text="市/縣"
- android:textSize="20dp"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Spinner
- android:id="@+id/city"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:text="省份/直轄市"
- android:textSize="20dp"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Spinner
- android:id="@+id/provinces"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:text="市/縣"
- android:textSize="20dp"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Spinner
- android:id="@+id/city"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
主程式City_SelectionActivity.java
- package com.cityselection;
- import java.io.File;
- 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.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class City_SelectionActivity extends Activity {
- /** Called when the activity is first created. */
- private File f = new File("/sdcard/weather/db_weather.db"); //資料庫檔案
- private Spinner province; //省份spinner
- private Spinner city; //城市spinner
- private List<String> proset=new ArrayList<String>();//省份集合
- private List<String> citset=new ArrayList<String>();//城市集合
- private int pro_id;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- province=(Spinner)findViewById(R.id.provinces);
- ArrayAdapter<String> pro_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getProSet());
- province.setAdapter(pro_adapter);
- province.setOnItemSelectedListener(new SelectProvince());
- city=(Spinner)findViewById(R.id.city);
- city.setOnItemSelectedListener(new SelectCity());
- }
- //選擇改變狀態
- class SelectProvince implements OnItemSelectedListener{
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- //獲得省份ID
- pro_id=position;
- city.setAdapter(getAdapter());
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
- //城市 選擇改變狀態
- class SelectCity implements OnItemSelectedListener{
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- String cityname=parent.getItemAtPosition(position).toString();
- //選擇提示
- Toast.makeText(getApplicationContext(), cityname+" "+getCityNum(position), 2000).show();
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
- /**
- * 返回 省份集合
- */
- public List<String> getProSet(){
- //開啟資料庫
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("provinces", null, null, null, null, null, null);
- while(cursor.moveToNext()){
- String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
- proset.add(pro);
- }
- cursor.close();
- db1.close();
- return proset;
- }
- /**
- * 返回 城市集合
- */
- public List<String> getCitSet(int pro_id){
- //清空城市集合
- citset.clear();
- //開啟資料庫
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("citys", null, "province_id="+pro_id, null, null, null, null);
- while(cursor.moveToNext()){
- String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
- citset.add(pro);
- }
- cursor.close();
- db1.close();
- return citset;
- }
- /**
- * 返回介面卡
- */
- public ArrayAdapter<String> getAdapter(){
- ArrayAdapter<String> adapter1=new ArrayAdapter(this, android.R.layout.simple_spinner_item,getCitSet(pro_id));
- return adapter1;
- }
- /**
- * 返回城市編號 以便呼叫天氣預報api
- */
- public long getCityNum(int position){
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("citys", null, "province_id="+pro_id, null, null, null, null);
- cursor.moveToPosition(position);
- long citynum=Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow("city_num")));
- cursor.close();
- db1.close();
- return citynum;
- }
- }
- package com.cityselection;
- import java.io.File;
- 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.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class City_SelectionActivity extends Activity {
- /** Called when the activity is first created. */
- private File f = new File("/sdcard/weather/db_weather.db"); //資料庫檔案
- private Spinner province; //省份spinner
- private Spinner city; //城市spinner
- private List<String> proset=new ArrayList<String>();//省份集合
- private List<String> citset=new ArrayList<String>();//城市集合
- private int pro_id;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- province=(Spinner)findViewById(R.id.provinces);
- ArrayAdapter<String> pro_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getProSet());
- province.setAdapter(pro_adapter);
- province.setOnItemSelectedListener(new SelectProvince());
- city=(Spinner)findViewById(R.id.city);
- city.setOnItemSelectedListener(new SelectCity());
- }
- //選擇改變狀態
- class SelectProvince implements OnItemSelectedListener{
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- //獲得省份ID
- pro_id=position;
- city.setAdapter(getAdapter());
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
- //城市 選擇改變狀態
- class SelectCity implements OnItemSelectedListener{
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- String cityname=parent.getItemAtPosition(position).toString();
- //選擇提示
- Toast.makeText(getApplicationContext(), cityname+" "+getCityNum(position), 2000).show();
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
- /**
- * 返回 省份集合
- */
- public List<String> getProSet(){
- //開啟資料庫
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("provinces", null, null, null, null, null, null);
- while(cursor.moveToNext()){
- String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
- proset.add(pro);
- }
- cursor.close();
- db1.close();
- return proset;
- }
- /**
- * 返回 城市集合
- */
- public List<String> getCitSet(int pro_id){
- //清空城市集合
- citset.clear();
- //開啟資料庫
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("citys", null, "province_id="+pro_id, null, null, null, null);
- while(cursor.moveToNext()){
- String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
- citset.add(pro);
- }
- cursor.close();
- db1.close();
- return citset;
- }
- /**
- * 返回介面卡
- */
- public ArrayAdapter<String> getAdapter(){
- ArrayAdapter<String> adapter1=new ArrayAdapter(this, android.R.layout.simple_spinner_item,getCitSet(pro_id));
- return adapter1;
- }
- /**
- * 返回城市編號 以便呼叫天氣預報api
- */
- public long getCityNum(int position){
- SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
- Cursor cursor=db1.query("citys", null, "province_id="+pro_id, null, null, null, null);
- cursor.moveToPosition(position);
- long citynum=Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow("city_num")));
- cursor.close();
- db1.close();
- return citynum;
- }
- }
實現結果:
程式碼下載 :http://download.csdn.net/detail/forsta/4248931
相關文章
- js根據ip地址獲取省份城市的方法JS
- vue實現城市列表選擇Vue
- React Native 實現城市選擇元件React Native元件
- android利用RecyclerView+自定義View實現城市選擇介面AndroidView
- android GPS 獲取城市資訊Android
- Flutter 側滑欄及城市選擇UI的實現FlutterUI
- 【Android】快速實現仿美團選擇城市介面,微信通訊錄介面Android
- android自定義佈局——城市選擇介面Android
- C# winfrom省份和城市 下拉選單聯動C#
- iOS開發之UITableView聯動實現城市選擇器iOSUIView
- php根據地理座標獲取國家、省份、城市,及周邊資料類PHP
- 根據ip獲取城市
- Flutter城市(省市區)選擇器Flutter
- iOS開發之城市選擇器iOS
- 基於 Flutter 的CityPickers 城市選擇器Flutter
- 利用js獲取IP,所在城市JS
- 微信小程式自定義元件-城市選擇微信小程式元件
- Uniapp 城市選擇JSON資料APPJSON
- 高德解析城市的分析,根據高德的經緯度獲取城市cityCode
- PHP獲取ip與ip所在城市PHP
- 微信小程式中根據字母選擇城市微信小程式
- js根據ip地址獲取所在城市JS
- jQuery選擇器獲取元素並非是動態jQuery
- 微信小程式--根據首字母選擇城市微信小程式
- HarmonyOS NEXT應用開發—城市選擇案例
- 無線城市如何在整個城市實現WiFi覆蓋WiFi
- 設計和編寫一個非同步通用Picker選擇器,用於時間日期、城市、商品分類的選擇非同步
- 基於cJSON及心知天氣模組化實現獲取城市氣象資訊(現在、未來)JSON
- js根據ip地址獲取城市地理位置JS
- js獲取使用者當前所在城市(ip)JS
- php獲取訪客所在城市名稱程式碼PHP
- 智慧城市:未來的城市
- python透過IP獲取國家和城市地市的方法Python
- EasyUI實現點選開啟編輯框並獲得焦點的方法UI
- 網頁裡引用mui的日曆,城市,自定義選擇標籤網頁UI
- three.js+vue3三維地圖下鑽地圖,實現下鑽全國-》省份-》城市-》區縣JSVue地圖
- 順元年智慧城市管理系統,實現城市數字化高效管理
- 實現城市列表的排序及模糊查詢排序