Exemple #1
0
  /**
   * @func 从db中读取某个省份的城市信息
   * @return 某个省份的城市名称列表
   */
  public List<City> loadCities(int provinceId) {
    List<City> list = new ArrayList<City>();

    Log.d("DEBUG", "load cities " + provinceId);

    // 此处还可以对查询条件进行限制

    Cursor cursor =
        db.query(
            "City",
            null,
            "province_id=?",
            new String[] {String.valueOf(provinceId)},
            null,
            null,
            null);
    Log.d("DEBUG", "load curosr " + provinceId);
    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());
    }

    if (cursor == null) {
      cursor.close();
    }

    return list;
  }
Exemple #2
0
 /**
  * @func 将City实例的数据写入到db
  * @param 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);
   }
 }