@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.fragment_choose_main); listview = (ListView) findViewById(R.id.list_view); titleText = (TextView) findViewById(R.id.title_text); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList); listview.setAdapter(adapter); coolweatherdb = CoolWeatherDB.getInstance(this); // listview的点击事件 listview.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) { if (currentLevel == LEVEL_PROVINCE) { selectedprovince = provinceList.get(index); querycity(); } else if (currentLevel == LEVEL_CITY) { selectedcity = cityList.get(index); querycounty(); } } }); queryprovince(); }
/* * 查询省内所有市,优先在数据库中查询,如果没有查询到再到服务器上去查询 */ private void querycity() { cityList = coolweatherdb.loadCities(selectedprovince.getId()); if (cityList.size() > 0) { dataList.clear(); for (City province : cityList) { dataList.add(province.getCityname()); } adapter.notifyDataSetChanged(); listview.setSelection(0); titleText.setText(selectedprovince.getProvincename()); currentLevel = LEVEL_CITY; } else { queryFromServer(selectedprovince.getProvincecode(), "city"); } }
/* * 查询全国所有的省,优先在数据库中查询,如果没有查询到再到服务器上去查询 */ private void queryprovince() { 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"); } }