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

 

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

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

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

 

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

 

Android 实现省份城市的选择,并获取城市编号_第1张图片

 

 

Android 实现省份城市的选择,并获取城市编号_第2张图片

Android 实现省份城市的选择,并获取城市编号_第3张图片

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

 

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>
 

布局文件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>
 

主程序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;
    }
    
}
 

 

实现结果:

Android 实现省份城市的选择,并获取城市编号_第4张图片

你可能感兴趣的:(android)