Android 实现省份城市的选择,并获取城市编号
2013-04-15 23:55
507 查看
该程序主要使用 中央气象局 省份 城市数据库为基础 进行读取
城市数据库下载 http://download.csdn.net/download/xianqiang1/3896880 感谢该兄弟的分享
下载的数据库 db_weather.db 放到sdcard/weather 目录下面 方便后续操作
为了更好的了解数据库,使用 SQLite Database Browser 可以打开数据库 查看数据 和表等信息,如下
了解了表的构成可以实现操作了
androidManifest.xml
配置文件声明 添加操作sdcard 权限
[html]
view plaincopyprint?
<?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 分别实现城市 省份的选择
[html]
view plaincopyprint?
<?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
[java]
view plaincopyprint?
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
转自:http://blog.csdn.net/forsta/article/details/7487229
城市数据库下载 http://download.csdn.net/download/xianqiang1/3896880 感谢该兄弟的分享
下载的数据库 db_weather.db 放到sdcard/weather 目录下面 方便后续操作
为了更好的了解数据库,使用 SQLite Database Browser 可以打开数据库 查看数据 和表等信息,如下
了解了表的构成可以实现操作了
androidManifest.xml
配置文件声明 添加操作sdcard 权限
[html]
view plaincopyprint?
<?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 分别实现城市 省份的选择
[html]
view plaincopyprint?
<?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
[java]
view plaincopyprint?
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
转自:http://blog.csdn.net/forsta/article/details/7487229
相关文章推荐
- Android 实现省份城市的选择,并获取城市编号
- android读取xml文件来实现省份,城市,区的选择
- android读取xml文件来实现省份,城市,区的选择
- android wheelview实现三级城市选择
- Android实现一个选择器-recycleview滚动中第一个item位置的获取
- Android上实现一个简单的天气预报APP(七) 切换到新的界面(选择城市界面)
- Android 自定义View实现城市选择列表
- 如何实现下拉选择省份后,后续单元格能选择对应的城市和县区
- Android自定义Dialog,实现性别选择,日期选择,获取EditText内容
- 调用腾讯接口,获取电脑ip,所在省份城市[PHP实现]
- Android上通过百度地图API实现选择城市/地点/距离/选择类型 来选择一定半径内的兴趣点
- android 如何使用spinner来实现选择省份和市区功能
- Android使用NumberPicker控件实现选择城市,生日
- 如何实现下拉选择省份后,后续单元格能选择对应的城市和县区
- 用Flex实现级联,实现选择省份,显示该省的所有城市
- jquery基于layui实现二级联动下拉选择(省份城市选择)
- Android实现自动定位城市并获取天气信息
- Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市
- 调用腾讯接口,获取电脑ip,所在省份城市[javascript实现]
- Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市