/** 查询选中市内所有的县,优先从数据库查询,如果没有查询到再去服务器上查询。 */ private void queryCounty() { countyList = weatherDB.loadCounty(selectedCity.getId()); if (countyList.size() > 0) { dataList.clear(); for (County county : countyList) { dataList.add(county.getCountyName()); } adapter.notifyDataSetChanged(); listView.setSelection(0); titleText.setText(selectedCity.getCityName()); currentLevel = LEVEL_COUNTY; } else { queryFromServer(selectedCity.getCityCode(), "county"); } }
/** 查询全国所有的省,优先从数据库查询,如果没有查询到再去服务器上查询。 */ private void queryProvinces() { provinceList = weatherDB.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"); } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); isFromWeather = getIntent().getBooleanExtra("from_weather_activity", false); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); if (sp.getBoolean("city_selected", false) && !isFromWeather) { Intent intent = new Intent(this, Weather.class); startActivity(intent); finish(); return; } requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.select_area); titleText = (TextView) findViewById(R.id.title_text); listView = (ListView) findViewById(R.id.list_view); adapter = new ArrayAdapter<String>(SelectArea.this, android.R.layout.simple_list_item_1, dataList); listView.setAdapter(adapter); weatherDB = WeatherDB.getInstance(this); listView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (currentLevel == LEVEL_PROVINCE) { selectedProvince = provinceList.get(position); queryCities(); } else if (currentLevel == LEVEL_CITY) { selectedCity = cityList.get(position); queryCounty(); } else if (currentLevel == LEVEL_COUNTY) { String countyCode = countyList.get(position).getCountyCode(); String countyName = countyList.get(position).getCountyName(); Intent intent = new Intent(SelectArea.this, Weather.class); intent.putExtra("county_code", countyCode); intent.putExtra("county_name", countyName); startActivity(intent); finish(); } } }); queryProvinces(); // 加载省级数据 }