歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 實現省份城市的選擇,並獲取城市編號

Android 實現省份城市的選擇,並獲取城市編號

日期:2017/3/1 9:55:21   编辑:Linux編程

Android 實現省份城市的選擇,並獲取城市編號。

該程序主要使用 中央氣象局 省份 城市數據庫為基礎 進行讀取

城市數據庫下載

免費下載地址在 http://linux.linuxidc.com/ (說明:此地址不是FTP,直接點擊打開,輸入用戶名與密碼)

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/7月/7日/Android 實現省份城市的選擇,並獲取城市編號

本文源碼下載

下載在Linux公社的1號FTP服務器裡,下載地址:

FTP地址:ftp://ftp1.linuxidc.com

用戶名:www.6688.cc

密碼:www.linuxidc.com

在 2013年LinuxIDC.com\7月\Android 實現省份城市的選擇,並獲取城市編號

下載方法見 http://www.linuxidc.net/thread-1187-1-1.html

-------------------------------------分割線-------------------------------------

下載的數據庫 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>

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

Copyright © Linux教程網 All Rights Reserved