private void queryProvinces() { mProvinceList = coolWeatherDB.loadProvinces(); if (mProvinceList.size() > 0) { dataList.clear(); for (Province province : mProvinceList) { dataList.add(province.getProvinceName()); } adapter.notifyDataSetChanged(); listview.setSelection(0); // ? titleText.setText("中国"); currentLevel = LEVEL_PROVINCE; } else { queryFromServer(null, "province"); } }
private void queryCounties() { mCountyList = coolWeatherDB.loadCounties(selectedCity.getId()); if (mCountyList.size() > 0) { dataList.clear(); for (County county : mCountyList) { dataList.add(county.getCountyName()); } adapter.notifyDataSetChanged(); listview.setSelection(0); titleText.setText(selectedCity.getCityName()); currentLevel = LEVEL_COUNTY; } else { queryFromServer(selectedCity.getCityCode(), "county"); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (prefs.getBoolean("city_selected", false)) { Intent intent = new Intent(this, WeatherActivity.class); startActivity(intent); finish(); } requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.choose_area); listview = (ListView) findViewById(R.id.list_view); titleText = (TextView) findViewById(R.id.title_text); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList); listview.setAdapter(adapter); coolWeatherDB = CoolWeatherDB.getInstance(this); listview.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (currentLevel == LEVEL_PROVINCE) { selectedProvince = mProvinceList.get(position); queryCities(); } else if (currentLevel == LEVEL_CITY) { selectedCity = mCityList.get(position); queryCounties(); } else if (currentLevel == LEVEL_COUNTY) { String countyCode = mCountyList.get(position).getCountyCode(); Intent intent = new Intent(ChooseAreaActivity.this, WeatherActivity.class); intent.putExtra("county_code", countyCode); startActivity(intent); finish(); } } }); queryProvinces(); }