Android 實現省份城市的選擇,並獲取城市編號

remotesupport發表於2014-08-24

該程式主要使用 中央氣象局 省份 城市資料庫為基礎 進行讀取

城市資料庫下載 http://download.csdn.net/download/xianqiang1/3896880 感謝該兄弟的分享

下載的資料庫 db_weather.db 放到sdcard/weather 目錄下面 方便後續操作

為了更好的瞭解資料庫,使用 SQLite Database Browser 可以開啟資料庫 檢視資料 和表等資訊,如下

瞭解了表的構成可以實現操作了

androidManifest.xml

配置檔案宣告 新增操作sdcard 許可權

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cityselection"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk android:minSdkVersion="8" />
  7. <!-- sdcard操作允許 -->
  8. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  9. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  10. <application
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name" >
  13. <activity
  14. android:name=".City_SelectionActivity"
  15. android:label="@string/app_name" >
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.cityselection"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.     <!-- sdcard操作允許 -->  
  9.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  10.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  11.     <application  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name" >  
  14.         <activity  
  15.             android:name=".City_SelectionActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.     </application>  
  24.   
  25. </manifest>  


佈局檔案main.xml

主要使用兩個 spinner 分別實現城市 省份的選擇

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:text="省份/直轄市"
  8. android:textSize="20dp"
  9. android:textStyle="bold"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. />
  13. <Spinner
  14. android:id="@+id/provinces"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. />
  18. <TextView
  19. android:text="市/縣"
  20. android:textSize="20dp"
  21. android:textStyle="bold"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. />
  25. <Spinner
  26. android:id="@+id/city"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. />
  30. </LinearLayout>
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:text="省份/直轄市"  
  9.         android:textSize="20dp"  
  10.         android:textStyle="bold"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.        />  
  14.     <Spinner  
  15.         android:id="@+id/provinces"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.        />  
  19.      <TextView  
  20.         android:text="市/縣"  
  21.         android:textSize="20dp"  
  22.         android:textStyle="bold"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.        />  
  26.     <Spinner  
  27.         android:id="@+id/city"  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         />  
  31.   
  32. </LinearLayout>  


主程式City_SelectionActivity.java

[java] view plaincopy
  1. package com.cityselection;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import android.app.Activity;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemSelectedListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.Spinner;
  14. import android.widget.Toast;
  15. public class City_SelectionActivity extends Activity {
  16. /** Called when the activity is first created. */
  17. private File f = new File("/sdcard/weather/db_weather.db"); //資料庫檔案
  18. private Spinner province; //省份spinner
  19. private Spinner city; //城市spinner
  20. private List<String> proset=new ArrayList<String>();//省份集合
  21. private List<String> citset=new ArrayList<String>();//城市集合
  22. private int pro_id;
  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. province=(Spinner)findViewById(R.id.provinces);
  28. ArrayAdapter<String> pro_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getProSet());
  29. province.setAdapter(pro_adapter);
  30. province.setOnItemSelectedListener(new SelectProvince());
  31. city=(Spinner)findViewById(R.id.city);
  32. city.setOnItemSelectedListener(new SelectCity());
  33. }
  34. //選擇改變狀態
  35. class SelectProvince implements OnItemSelectedListener{
  36. public void onItemSelected(AdapterView<?> parent, View view,
  37. int position, long id) {
  38. // TODO Auto-generated method stub
  39. //獲得省份ID
  40. pro_id=position;
  41. city.setAdapter(getAdapter());
  42. }
  43. public void onNothingSelected(AdapterView<?> arg0) {
  44. // TODO Auto-generated method stub
  45. }
  46. }
  47. //城市 選擇改變狀態
  48. class SelectCity implements OnItemSelectedListener{
  49. public void onItemSelected(AdapterView<?> parent, View view,
  50. int position, long id) {
  51. // TODO Auto-generated method stub
  52. String cityname=parent.getItemAtPosition(position).toString();
  53. //選擇提示
  54. Toast.makeText(getApplicationContext(), cityname+" "+getCityNum(position), 2000).show();
  55. }
  56. public void onNothingSelected(AdapterView<?> arg0) {
  57. // TODO Auto-generated method stub
  58. }
  59. }
  60. /**
  61. * 返回 省份集合
  62. */
  63. public List<String> getProSet(){
  64. //開啟資料庫
  65. SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
  66. Cursor cursor=db1.query("provinces"nullnullnullnullnullnull);
  67. while(cursor.moveToNext()){
  68. String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
  69. proset.add(pro);
  70. }
  71. cursor.close();
  72. db1.close();
  73. return proset;
  74. }
  75. /**
  76. * 返回 城市集合
  77. */
  78. public List<String> getCitSet(int pro_id){
  79. //清空城市集合
  80. citset.clear();
  81. //開啟資料庫
  82. SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
  83. Cursor cursor=db1.query("citys"null"province_id="+pro_id, nullnullnullnull);
  84. while(cursor.moveToNext()){
  85. String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));
  86. citset.add(pro);
  87. }
  88. cursor.close();
  89. db1.close();
  90. return citset;
  91. }
  92. /**
  93. * 返回介面卡
  94. */
  95. public ArrayAdapter<String> getAdapter(){
  96. ArrayAdapter<String> adapter1=new ArrayAdapter(this, android.R.layout.simple_spinner_item,getCitSet(pro_id));
  97. return adapter1;
  98. }
  99. /**
  100. * 返回城市編號 以便呼叫天氣預報api
  101. */
  102. public long getCityNum(int position){
  103. SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
  104. Cursor cursor=db1.query("citys"null"province_id="+pro_id, nullnullnullnull);
  105. cursor.moveToPosition(position);
  106. long citynum=Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow("city_num")));
  107. cursor.close();
  108. db1.close();
  109. return citynum;
  110. }
  111. }
[java] view plaincopy
  1. package com.cityselection;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemSelectedListener;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.Spinner;  
  16. import android.widget.Toast;  
  17.   
  18. public class City_SelectionActivity extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.       
  21.     private File f = new File("/sdcard/weather/db_weather.db"); //資料庫檔案  
  22.       
  23.     private Spinner province;  //省份spinner  
  24.     private Spinner city;      //城市spinner  
  25.       
  26.     private List<String> proset=new ArrayList<String>();//省份集合  
  27.     private List<String> citset=new ArrayList<String>();//城市集合  
  28.       
  29.     private int pro_id;  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.           
  35.         province=(Spinner)findViewById(R.id.provinces);  
  36.         ArrayAdapter<String> pro_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,getProSet());  
  37.         province.setAdapter(pro_adapter);  
  38.         province.setOnItemSelectedListener(new SelectProvince());  
  39.           
  40.         city=(Spinner)findViewById(R.id.city);  
  41.         city.setOnItemSelectedListener(new SelectCity());  
  42.     }  
  43.      
  44.     //選擇改變狀態  
  45.     class SelectProvince implements OnItemSelectedListener{  
  46.         public void onItemSelected(AdapterView<?> parent, View view,  
  47.                 int position, long id) {  
  48.             // TODO Auto-generated method stub  
  49.             //獲得省份ID  
  50.             pro_id=position;          
  51.             city.setAdapter(getAdapter());  
  52.               
  53.         }  
  54.         public void onNothingSelected(AdapterView<?> arg0) {  
  55.             // TODO Auto-generated method stub  
  56.               
  57.         }  
  58.     }  
  59.       
  60.     //城市 選擇改變狀態  
  61.     class SelectCity implements OnItemSelectedListener{  
  62.         public void onItemSelected(AdapterView<?> parent, View view,  
  63.                 int position, long id) {  
  64.             // TODO Auto-generated method stub  
  65.             String cityname=parent.getItemAtPosition(position).toString();  
  66.             //選擇提示  
  67.             Toast.makeText(getApplicationContext(), cityname+" "+getCityNum(position), 2000).show();  
  68.               
  69.             
  70.         }  
  71.         public void onNothingSelected(AdapterView<?> arg0) {  
  72.             // TODO Auto-generated method stub  
  73.               
  74.         }  
  75.     }  
  76.       
  77.     /** 
  78.      * 返回 省份集合 
  79.      */  
  80.     public List<String> getProSet(){  
  81.        //開啟資料庫   
  82.         SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);  
  83.         Cursor cursor=db1.query("provinces"nullnullnullnullnullnull);  
  84.         while(cursor.moveToNext()){  
  85.             String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));  
  86.             proset.add(pro);  
  87.         }  
  88.         cursor.close();  
  89.         db1.close();  
  90.         return proset;  
  91.     }  
  92.     /** 
  93.      * 返回 城市集合 
  94.      */  
  95.     public List<String> getCitSet(int pro_id){  
  96.         //清空城市集合  
  97.         citset.clear();  
  98.        //開啟資料庫   
  99.         SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);  
  100.         Cursor cursor=db1.query("citys"null"province_id="+pro_id, nullnullnullnull);  
  101.         while(cursor.moveToNext()){  
  102.             String pro=cursor.getString(cursor.getColumnIndexOrThrow("name"));  
  103.             citset.add(pro);  
  104.         }  
  105.         cursor.close();  
  106.         db1.close();  
  107.         return citset;  
  108.     }  
  109.     /** 
  110.      * 返回介面卡 
  111.      */  
  112.     public ArrayAdapter<String> getAdapter(){  
  113.           ArrayAdapter<String> adapter1=new ArrayAdapter(this, android.R.layout.simple_spinner_item,getCitSet(pro_id));  
  114.           return adapter1;  
  115.     }  
  116.      /** 
  117.       * 返回城市編號  以便呼叫天氣預報api 
  118.       */  
  119.     public long getCityNum(int position){  
  120.         SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);  
  121.         Cursor cursor=db1.query("citys"null"province_id="+pro_id, nullnullnullnull);  
  122.         cursor.moveToPosition(position);  
  123.         long citynum=Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow("city_num")));  
  124.         cursor.close();  
  125.         db1.close();  
  126.         return citynum;  
  127.     }  
  128.       
  129. }  


實現結果:

程式碼下載 :http://download.csdn.net/detail/forsta/4248931

 


相關文章