Beispiel #1
0
 /**
  * @func 将Country实例的数据存储到db
  * @param country
  */
 public void saveCountry(Country country) {
   if (country != null) {
     ContentValues values = new ContentValues();
     values.put("country_name", country.getCountryName());
     values.put("country_code", country.getCountryCode());
     values.put("city_id", country.getCityId());
     db.insert("Country", null, values);
   }
 }
Beispiel #2
0
  /**
   * @func 从db中读取某个城市的乡镇信息
   * @return 某个城市的乡镇名称列表
   */
  public List<Country> loadCounties(int cityId) {
    List<Country> list = new ArrayList<Country>();

    // 此处还可以对查询条件进行限制
    Cursor cursor =
        db.query(
            "Country", null, "city_id=?", new String[] {String.valueOf(cityId)}, null, null, null);
    if (cursor.moveToFirst()) {
      do {
        Country country = new Country();
        country.setId(cursor.getInt(cursor.getColumnIndex("id")));
        country.setCountryName(cursor.getString(cursor.getColumnIndex("country_name")));
        country.setCountryCode(cursor.getString(cursor.getColumnIndex("country_code")));
        country.setCityId(cityId);
        list.add(country);
      } while (cursor.moveToNext());
    }

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

    return list;
  }