/** * 根据provinceId 加载City数据 * * @return */ public List<City> loadCities(int provinceId) { List<City> cityList = new ArrayList<>(); 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(cursor.getInt(cursor.getColumnIndex("province_id"))); cityList.add(city); } while (cursor.moveToNext()); } return cityList; }
/** * 保存City数据 * * @param city */ public void saveCity(City city) { if (null != city) { 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); } }