概述
该程序主要使用 中央气象局 省份 城市数据库为基础 进行读取
城市数据库下载 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
最后
以上就是淡然白猫为你收集整理的Android 实现省份城市的选择,并获取城市编号的全部内容,希望文章能够帮你解决Android 实现省份城市的选择,并获取城市编号所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复