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

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

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

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

androidManifest.xml

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

package="com.cityselection"

android:versionCode="1"

android:versionName="1.0">

android:icon="@drawable/ic_launcher"

android:label="@string/app_name">

android:name=".City_SelectionActivity"

android:label="@string/app_name">

package="com.cityselection"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".City_SelectionActivity"

android:label="@string/app_name" >

布局文件main.xml

主要使用两个 spinner 分别实现城市 省份的选择

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:text="省份/直辖市"

android:textSize="20dp"

android:textStyle="bold"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/provinces"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:text="市/县"

android:textSize="20dp"

android:textStyle="bold"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/city"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:text="省份/直辖市"

android:textSize="20dp"

android:textStyle="bold"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/provinces"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:text="市/县"

android:textSize="20dp"

android:textStyle="bold"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

android:id="@+id/city"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

主程序City_SelectionActivity.java

packagecom.cityselection;

importjava.io.File;

importjava.util.ArrayList;

importjava.util.List;

importandroid.app.Activity;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.AdapterView;

importandroid.widget.AdapterView.OnItemSelectedListener;

importandroid.widget.ArrayAdapter;

importandroid.widget.Spinner;

importandroid.widget.Toast;

publicclassCity_SelectionActivityextendsActivity {

/** Called when the activity is first created. */

privateFile f = newFile("/sdcard/weather/db_weather.db");//数据库文件

privateSpinner province;//省份spinner

privateSpinner city; //城市spinner

privateList proset=newArrayList();//省份集合

privateList citset=newArrayList();//城市集合

privateintpro_id;

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

province=(Spinner)findViewById(R.id.provinces);

ArrayAdapter pro_adapter=newArrayAdapter(this, android.R.layout.simple_spinner_item,getProSet());

province.setAdapter(pro_adapter);

province.setOnItemSelectedListener(newSelectProvince());

city=(Spinner)findViewById(R.id.city);

city.setOnItemSelectedListener(newSelectCity());

}

//选择改变状态

classSelectProvinceimplementsOnItemSelectedListener{

publicvoidonItemSelected(AdapterView> parent, View view,

intposition, longid) {

// TODO Auto-generated method stub

//获得省份ID

pro_id=position;

city.setAdapter(getAdapter());

}

publicvoidonNothingSelected(AdapterView> arg0) {

// TODO Auto-generated method stub

}

}

//城市 选择改变状态

classSelectCity implementsOnItemSelectedListener{

publicvoidonItemSelected(AdapterView> parent, View view,

intposition, longid) {

// TODO Auto-generated method stub

String cityname=parent.getItemAtPosition(position).toString();

//选择提示

Toast.makeText(getApplicationContext(), cityname+" "+getCityNum(position),2000).show();

}

publicvoidonNothingSelected(AdapterView> arg0) {

// TODO Auto-generated method stub

}

}

/**

* 返回 省份集合

*/

publicList 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();

returnproset;

}

/**

* 返回 城市集合

*/

publicList getCitSet(intpro_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();

returncitset;

}

/**

* 返回适配器

*/

publicArrayAdapter getAdapter(){

ArrayAdapter adapter1=newArrayAdapter(this, android.R.layout.simple_spinner_item,getCitSet(pro_id));

returnadapter1;

}

/**

* 返回城市编号 以便调用天气预报api

*/

publiclonggetCityNum(intposition){

SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f,null);

Cursor cursor=db1.query("citys",null,"province_id="+pro_id,null,null,null,null);

cursor.moveToPosition(position);

longcitynum=Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow("city_num")));

cursor.close();

db1.close();

returncitynum;

}

}

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 proset=new ArrayList();//省份集合

private List citset=new ArrayList();//城市集合

private int pro_id;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

province=(Spinner)findViewById(R.id.provinces);

ArrayAdapter pro_adapter=new ArrayAdapter(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 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 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 getAdapter(){

ArrayAdapter 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 省份城市搜索,Android 实现省份城市的选择,并获取城市编号相关推荐

  1. Android 省份城市搜索,android - 非常不错的 城市省份的选择组件: citypicker

    android - 非常不错的 城市省份的选择组件: citypicker 2017-11-03 11:59 访问量: 2184 分类: 技术 用法: 1.  修改根目录下的build.gradle, ...

  2. android定位并获取城市

    在项目中需要定位当前用户所在城市,然后根据不同城市返回不同的数据.一般来说,定位有两种方式,1.用第三方的定位sdk,如百度定位:2.用android自带的sdk中的api定位. 一.用百度SDK定位 ...

  3. Android根据经纬度获取城市名的方法

    根据经纬度获取当前城市名的几种方式如下(获取城市名需要网络连接) 1. 通过服务获取城市名 百度: http://api.map.baidu.com/geocoder?output=json& ...

  4. android 输入法确定键,android 改变输入法enter键文字 为搜索 下一个 以及前往

    我们大家都知道通过指定EditText的android:imeOptions属性可以修改 输入法enter键的显示情况 例如: android:imeOptions="actionNext& ...

  5. .NET获取城市信息(将三字代码转换成城市名)

    整理代码,发现有一个从两张表里读取城市列表,然后linq和lambda表达式来获取城市名的函数,代码如下: 1 public static string GetCityHotelText(string ...

  6. weather_在Weather App中使用Android Location API –搜索城市

    weather 在这篇文章中,我想描述如何使用openweathermap搜索城市以获取天气状况. 我们可以通过两种方式查找城市: 使用名称模式 使用地理坐标(Android Location API ...

  7. Android 天气APP(十五)增加城市搜索、历史搜索记录

    上一篇:Android 天气APP(十四)修复UI显示异常.优化业务代码逻辑.增加详情天气显示 添加城市 新版------------------- 一.推荐城市数据 二.推荐城市item布局和适配器 ...

  8. [Android精品源码] Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市

    Material Design中文版Code4APPPHP100UI4APP 开启辅助访问设为首页收藏本站快捷导航切换到宽版切换风格 石刚 | |我的 |签到打卡 |设置 |消息 |提醒(2) |退出 ...

  9. Android 城市选择增加热门城市

    Android 城市选择  可用于第一次进入应用程序定位失败后手动选择城市. 实现代码 <pre name="code" class="java"> ...

最新文章

  1. navicat连接云数据库报错2003,2005
  2. python3 x完全兼容_【转】Python3.x与Python2.x的主要区别 (O_O )?
  3. 造一个鸿蒙,仅有华为还不够
  4. mysql的检查点_转载一篇关于mysql检查点的文章
  5. 挨踢脱口秀精选集汇总
  6. C++判断一个数是否为回文数palindrome的算法(附完整源码)
  7. 【Matlab】矩阵三角分解法求解方程组
  8. go语言map按照key,value进行排序
  9. 监听localStorage变化(同页面监听)
  10. linux中xy是什么命令,Linux命令中chmod 777 以及drwxr-xr-x分别代表什么意思
  11. 数字金融反欺诈技术名词表
  12. Glib2之spec编译打包rpm(九)
  13. Java运行Python脚本的几种方式
  14. android mmkv使用_[Android]高性能MMKV数据交互分析-MMKV初始化
  15. aix oracle 10.2.0.1 升级 10.2.0.4,【江枫 】AIX平台升级到Oracle10.2.0.4的几个问题
  16. Ubuntu1804编译QWebEngine
  17. 操作系统的中的 IO
  18. vue.js 密码加密_Word2007/2016/2019文档加密的方法
  19. docker存储bind mounts用法
  20. 我的 2020 总结,我在蚂蚁成长的这一年

热门文章

  1. keil5生成一个单片机led呼吸灯程序
  2. mac电脑maven阿里云镜像配置
  3. JLX12864G液晶显示屏驱动
  4. 【第十一届“泰迪杯”数据挖掘挑战赛】泰迪杯c题爬虫采集数据(源码+数据)
  5. Python爬取全国主要城市经纬度坐标
  6. xxx.dll文件缺失修复方法
  7. cad批量打印_CAD批量打印(分图大师)
  8. BGP路由协议的那些事?(中)
  9. 基于FPGA的QPSK调制系统verilog开发
  10. 如何扩大ubuntu的ubuntu--vg-ubuntu--lv空间