Android 实现省份城市的选择,并获取城市编号

本文详细介绍了如何使用中央气象局提供的省份城市数据库开发一款Android应用,实现城市天气信息查询。通过配置AndroidManifest.xml文件声明权限、布局文件设计两个spinner实现城市选择,并通过SQLiteDatabase操作数据库获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该程序主要使用 中央气象局 省份 城市数据库为基础 进行读取

城市数据库下载 http://download.youkuaiyun.com/download/xianqiang1/3896880 感谢该兄弟的分享

下载的数据库 db_weather.db 放到sdcard/weather 目录下面 方便后续操作

为了更好的了解数据库,使用 SQLite Database Browser 可以打开数据库 查看数据 和表等信息,如下

了解了表的构成可以实现操作了

androidManifest.xml

配置文件声明 添加操作sdcard 权限

  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>
<?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 分别实现城市 省份的选择

  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>
<?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

  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", null, null, null, null, null, null);
  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, null, null, null, null);
  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, null, null, null, null);
  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. }
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.youkuaiyun.com/detail/forsta/4248931

 

转自:http://blog.youkuaiyun.com/forsta/article/details/7487229

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值