本次博文将详解一个天气预报APP,找了好多种方式去获取天气数据,最后发现通过访问中国天气网服务器提供的免费接口这种方法来获取数据最为简单。本博文项目就是通过这种方式进行程序开发的。
首先要了解一下APP的目标 功能:
1.可以罗列出全国所有的省,市,县。
2.可以查看任意全国任意城市的天气信息。
3.可以自由的切换城市,去查看其它城市的天气
4.提供手工更新及后台更新天气的功能。
建立工程,首先在工程里创建以下的包。
第一阶段就是要创建程序所需要的数据库和表
这里我们建立三张表,Province,City,County,分别用来存放省,市,县的各种信息。
首先在db包中新建一个CoolWeatherOpenHelper的类,代码如下
public class CoolWeatherOpenHelper extends SQLiteOpenHelper{
/*
* *province表示建表语句
*/
public static final String CREATE_PROVINCE="create table Province ("
+"id integer primary key autoincrement, "
+"province_name text, "
+"province_code text)";
/*
* *city表示建表语句
*/
public static final String CREATE_CITY="create table City ("
+"id integer primary key autoincrement, "
+"city_name text, "
+"city_code text, "
+"province_id integer)";
/*
* *county表示建表语句
*/
public static final String CREATE_COUNTY="create table County ("
+"id integer primary key autoincrement, "
+"county_name text, "
+"county_code text, "
+"city_id integer)";
public CoolWeatherOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_PROVINCE);//创建Province表
db.execSQL(CREATE_CITY);//创建City表
db.execSQL(CREATE_COUNTY);//创建County表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
另外,每张表在代码中最好能有一个对应的实体类,方便后续的开发工作。分别在model包下建立三个类。
Priovince类:
public class Province {
private int id;
private String provinceName;
private String ProvinceCode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getProvinceCode() {
return ProvinceCode;
}
public void setProvinceCode(String provinceCode) {
ProvinceCode = provinceCode;
}
}
City类:
public class City {
private int id;
private String cityName;
private String cityCode;
private int provinceId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public int getProvinceId() {
return provinceId;
}
public void setProvinceId(int provinceId) {
this.provinceId = provinceId;
}
}
County类:
public class County {
private int id;
private String countyName;
private String countyCode;
private int cityId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountyName() {
return countyName;
}
public void setCountyName(String countyName) {
this.countyName = countyName;
}
public String getCountyCode() {
return countyCode;
}
public void setCountyCode(String countyCode) {
this.countyCode = countyCode;
}
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
}
接着我们还需要一个CoolWeatherDB类,将一些常用的数据库操作封装起来,代码如下
public class CoolWeatherDB {
/*
* 数据库名
*/
private static final String DB_NAME="cool_weather";
/*
* 数据库版本
*/
public static final int VERSION=1;
private static CoolWeatherDB coolWeatherDB;
private SQLiteDatabase db;
/*
* 将构造方法私有化
*/
private CoolWeatherDB(Context context)
{
CoolWeatherOpenHelper dbHelper=new CoolWeatherOpenHelper(context,DB_NAME,null,VERSION);
db=dbHelper.getWritableDatabase();
}
/*
* 获取CollWeatherDB的实例
*/
public synchronized static CoolWeatherDB getInstance(Context context)
{
if(coolWeatherDB==null)
{
coolWeatherDB=new CoolWeatherDB(context);
}
return coolWeatherDB;
}
/*
* 将Province实例储存到数据库
*/
public void saveProvince(Province province)
{
if(province!=null)
{
ContentValues values=new ContentValues();
values.put("province_name", province.getProvinceName());
values.put("province_code", province.getProvinceCode());
db.insert("Province", null, values);
}
}
/*
* 从数据库读取全国所有省份的信息
*/
public List<Province> loadProvinces()
{
List<Province> list=new ArrayList<Province>();
Cursor cursor=db.query("Province", null, null, null, null, null, null);
if(cursor.moveToFirst())
{
do{
Province province=new Province();
province.setId(cursor.getInt(cursor.getColumnIndex("id")));
province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
list.add(province);
}while(cursor.moveToNext());
}
return list;
}
/*
* 将City实例储存到数据库
*/
public void saveCity(City city)
{
if(city!=null)
{
ContentValues values=new ContentValues();
values.put("city_name", city.getCityName());
values.put("city_code", city.getCityCode());
values.put("province_id", city.getProvinceId());
db.insert("City", null, values);
}
}
/*
* 从数据库中读取省下所有城市信息
*/
public List<City> loadCities(int provinceId)
{
List<City> list=new ArrayList<City>();
Cursor cursor=db.query("City", null, "province_id=?", new String[]{String.valueOf(provinceId)}, null, null, null);
if(cursor.moveToFirst())
{
do{
City city=new City();
city.setId(cursor.getInt(cursor.getColumnIndex("id")));
city.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code")));
city.setProvinceId(provinceId);
list.add(city);
}while(cursor.moveToNext());
}
return list;
}
/*
* 将County实例储存到数据库中。
*/
public void saveCounty(County county)
{
if(county!=null)
{
ContentValues values=new ContentValues();
values.put("county_name", county.getCountyName());
values.put("county_code", county.getCountyCode());
values.put("city_id", county.getCityId());
db.insert("County", null, values);
}
}
/*
* 从数据库中读取某城市中的所有县城信息
*/
public List<County> loadCounties(int cityId)
{
List<County> list=new ArrayList<County>();
Cursor cursor=db.query("County", null, "city_id=?", new String[]{String.valueOf(cityId)}, null, null, null);
if(cursor.moveToFirst())
{
do{
County county=new County();
county.setId(cursor.getInt(cursor.getColumnIndex("id")));
county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
county.setCityId(cityId);
list.add(county);
}while(cursor.moveToNext());
}
return list;
}
}
可以看到,CoolWeatherDB是一个单例类,我们将它的构造方法私有化,并提供一个getInstance()方法来获取CoolWeatherDB实例,接下来就可以调用
CoolWeatherDB里的方法来储存所有省份,市,县级等数据及读取所有省,市,县级的数据。
好了,第一阶段的代码就写到这里了,是不是都比较好理解! 敬请期待第二阶段的代码。