/** 查询全国所有的省,优先从数据库查询,如果没有查询到再去服务器上查询。 */ private void queryProvinces() { provinceList = coolWeatherDB.loadProvinces(); if (provinceList.size() > 0) { dataList.clear(); for (Province province : provinceList) { dataList.add(province.getProvinceName()); } adapter.notifyDataSetChanged(); listView.setSelection(0); titleText.setText("中国"); currentLevel = LEVEL_PROVINCE; } else { queryFromServer(null, "province"); } }
/** 查询选中省内所有的市,优先从数据库查询,如果没有查询到再去服务器上查询。 */ private void queryCities() { cityList = coolWeatherDB.loadCities(selectedProvince.getId()); if (cityList.size() > 0) { dataList.clear(); for (City city : cityList) { dataList.add(city.getCityName()); } adapter.notifyDataSetChanged(); listView.setSelection(0); titleText.setText(selectedProvince.getProvinceName()); currentLevel = LEVEL_CITY; } else { queryFromServer(selectedProvince.getProvinceCode(), "city"); } }
/** 解析和处理服务器返回的省级数据 */ public static synchronized boolean handleProvincesResponse( CoolWeatherDB coolWeatherDB, String response) { if (!TextUtils.isEmpty(response)) { String[] allProvinces = response.split(","); if (allProvinces != null && allProvinces.length > 0) { for (String p : allProvinces) { String[] array = p.split("\\|"); Province province = new Province(); province.setProvinceCode(array[0]); province.setProvinceName(array[1]); // 将解析出来的数据存储到Province表 coolWeatherDB.saveProvince(province); } return true; } } return false; }