之前写了一款基于Android的空空天气预报,能够查看全国各个省、市、县的未来三天的天气预报,有穿衣指数和运动指数建议;

最近准备找工作了,现在重新回顾一下思路。

主要用到的知识有解析xml技术,解析json,mvc架构。

1、首先看下界面结果;

 空空天气预报空空天气预报空空天气预报

 

2、代码思路

2-1 代码架构图

空空天气预报

 

2-2 设计思路

2-2-1 首先解析一个省市县xml文件,里面的代码样式如图:

  <province id="01" name="北京">
    <city id="0101" name="北京">
      <county id="010101" name="北京" weatherCode="101010100"/>
      <county id="010102" name="海淀" weatherCode="101010200"/>
      <county id="010103" name="朝阳" weatherCode="101010300"/>
      <county id="010104" name="顺义" weatherCode="101010400"/>
      <county id="010105" name="怀柔" weatherCode="101010500"/>
      <county id="010106" name="通州" weatherCode="101010600"/>
      <county id="010107" name="昌平" weatherCode="101010700"/>
      <county id="010108" name="延庆" weatherCode="101010800"/>
      <county id="010109" name="丰台" weatherCode="101010900"/>
      <county id="010110" name="石景山" weatherCode="101011000"/>
      <county id="010111" name="大兴" weatherCode="101011100"/>
      <county id="010112" name="房山" weatherCode="101011200"/>
      <county id="010113" name="密云" weatherCode="101011300"/>
      <county id="010114" name="门头沟" weatherCode="101011400"/>
      <county id="010115" name="平谷" weatherCode="101011500"/>
    </city>
  </province>

2-2-2 模型层的建立

主要是在model层里面建立City、County、Province三个类,下面贴一部分代码:

public class County {
    private int id;//县城主id
    private String countyName;//县城名
    private String countyCode;//县城代码
    private int cityId;//城市id
    ...
public class City {
    private int id;//城市id
    private String cityName;//城市名称
    private String cityCode;//城市代码
    private int provinceId;//省id
        ...
public class Province {
    private int id;//省id
    private String provinceName;//省名
    private String provinceCode;//省代码
    public int getId() {
        ...

2-2-3 数据库的设计

设计两个类,一个类是CoolWeatherOpenHelper 继承自 SQLiteOpenHelper,用于数据库的创建和更新,大致代码如下:

public class CoolWeatherOpenHelper extends SQLiteOpenHelper {
    
    public CoolWeatherOpenHelper(Context context, String name,CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    //创建省、市、县的sql语句
    public static final String CREATE_PROVINCE = 
                  "create table Province ("
                + "id integer primary key autoincrement,"
                + "provinceName text,"
                + "provinceCode text)";
    public static final String CREATE_CITY = 
                  "create table City ("
                + "id integer primary key autoincrement,"
                + "cityName text,"
                + "cityCode text,"
                + "provinceId integer)";
    public static final String CREATE_COUNTY = 
                  "create table County ("
                + "id integer primary key autoincrement,"
                + "countyName text,"
                + "countyCode text,"
                + "cityId integer)";
    //创建数据库            
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_PROVINCE); //create tables
        db.execSQL(CREATE_CITY);
        db.execSQL(CREATE_COUNTY);
    }
...

设计第二个类CoolWeatherDB,用于插入xml文件中省市县的天气预报代码

  1 public class CoolWeatherDB {
  2     /**
  3      * database name 数据库名
  4      */
  5     public static final String DB_NAME = "cool_weather.db";
  6     /**
  7      * database version ,database is used for operating the database (insert , update, delete, select)
  8      */
  9     public static final int VERSION = 1;
 10     private static CoolWeatherDB coolWeatherDB;
 11     private SQLiteDatabase db;
 12     
 13     /**
 14      * constructor构造函数
 15      */
 16     private CoolWeatherDB(Context context) {
 17         CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION);
 18         db = dbHelper.getWritableDatabase();//call onCreate method
 19     }
 20     /**
 21      * get the instance of CoolWeatherDB, use the singleInstance modern
 22      * 获得CoolWeatherDB对象,这里使用了单例模式
 23      */
 24     public  synchronized static CoolWeatherDB getInstance(Context context) {
 25         if(coolWeatherDB == null)
 26             coolWeatherDB = new CoolWeatherDB(context);
 27         return coolWeatherDB;
 28     }
 29     /**
 30      * insert province information to the database
 31      * 插入省信息到数据库中
 32      */
 33     public void saveProvince(Province province) {
 34         if(province != null) {
 35             ContentValues values = new ContentValues();
 36             values.put("provinceName", province.getProvinceName());
 37             values.put("provinceCode", province.getProvinceCode());
 38             db.insert("Province", null, values);
 39         }
 40     }
 41     /**
 42      * insert a list of provinces
 43      * 插入一系列省信息到数据库中
 44      * @param provinces
 45      */
 46     public void saveMangProvinces(ArrayList<Province> provinces) {
 47         if(provinces.size() > 0) {
 48             for(Province province: provinces) {
 49                 ContentValues values = new ContentValues();
 50                 values.put("provinceName", province.getProvinceName());
 51                 values.put("provinceCode", province.getProvinceCode());
 52                 db.insert("Province", null, values);
 53             }
 54         }
 55     }
 56     /**
 57      * save city
 58      * 插入城市信息到数据库中
 59      */
 60     public void saveCity(City city) {
 61         if(city != null) {
 62             ContentValues values = new ContentValues();
 63             values.put("cityName", city.getCityName());
 64             values.put("cityCode", city.getCityCode());
 65             values.put("provinceId", city.getProvinceId());
 66             db.insert("City", null, values);
 67         }
 68     }
 69     /**
 70      * insert a list of cities
 71      * 插入一系列的城市信息到数据库中
 72      * @param cities
 73      */
 74     public void saveMangCities(ArrayList<City> cities) {
 75         
 76         if(cities.size() > 0) {
 77             for(City city: cities) {
 78                 ContentValues values = new ContentValues();
 79                 values.put("cityName", city.getCityName());
 80                 values.put("cityCode", city.getCityCode());
 81                 values.put("provinceId", city.getProvinceId());
 82                 db.insert("City", null, values);
 83             }
 84         }
 85     }
 86     /**
 87      * save county
 88      * 插入县信息到数据库中
 89      */
 90     public void saveCounty(County county) {
 91         if(county != null) {
 92             ContentValues values = new ContentValues();
 93             values.put("countyName", county.getCountyName());
 94             values.put("countyCode", county.getCountyCode());
 95             values.put("cityId", county.getCityId());
 96             db.insert("County", null, values);
 97         }
 98     }
 99     /**
100      * 插入一系列县信息到数据库中
101      * @param counties
102      */
103     public void saveMangCounties(ArrayList<County> counties) {
104         if(counties.size() > 0) {
105             for(County county: counties) {
106                 ContentValues values = new ContentValues();
107                 values.put("countyName", county.getCountyName());
108                 values.put("countyCode", county.getCountyCode());
109                 values.put("cityId", county.getCityId());
110                 db.insert("County", null, values);
111             }
112         }
113     }
114     /**
115      * get province infor from the db
116      * 从数据库中获取省信息
117      */
118     public List<Province> loadProvinces() {
119         List<Province> list = new ArrayList<Province>();
120         Cursor cursor = db.query("Province", null, null, null, null, null, null);
121         if(cursor.moveToFirst()) {
122             do{
123                 Province province = new Province();
124                 province.setId(cursor.getInt(cursor.getColumnIndex("id")));
125                 province.setProvinceName(cursor.getString(cursor.getColumnIndex("provinceName")));
126                 province.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
127                 list.add(province);
128             }while(cursor.moveToNext());
129         }
130         return list;
131     }
132     /**
133      * get city infor from the db by provinceId
134      * 根据省id从数据库中获取城市信息
135      */
136     public List<City> loadCities(int provinceId) {
137         List<City> list = new ArrayList<City>();
138         Cursor cursor = db.query("City", null, "provinceId=?", new String[] {String.valueOf(provinceId)}, null, null, null);
139         if(cursor.moveToFirst()) {
140             do{
141                 City city= new City();
142                 city.setId(cursor.getInt(cursor.getColumnIndex("id")));
143                 city.setCityName(cursor.getString(cursor.getColumnIndex("cityName")));
144                 city.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
145                 list.add(city);
146             }while(cursor.moveToNext());
147         }
148         return list;
149     }
150     /**
151      * get county information from the database by cityId
152      * 根据城市id从数据库中获取县城信息
153      */
154     public List<County> loadCounties(String cityCode) {
155         List<County> list = new ArrayList<County>();
156         Cursor cursor = db.query("County", null, "cityId=?", new String[]{String.valueOf(cityCode)}, null, null, null);
157         if(cursor.moveToFirst()) {
158             do{
159                 County county = new County();
160                 county.setId(cursor.getInt(cursor.getColumnIndex("id")));
161                 county.setCountyName(cursor.getString(cursor.getColumnIndex("countyName")));
162                 county.setCountyCode(cursor.getString(cursor.getColumnIndex("countyCode")));
163                 county.setCityId(Integer.parseInt(cityCode));
164                 list.add(county);
165             }while(cursor.moveToNext());
166         }
167         return list;
168     }
169 }
View Code

相关文章: