Back to project page firstcodeandroid.
The source code is released under:
MIT License
If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.vjia.coolweather.activity; /*from www . j a va 2s. c o m*/ import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.TextUtils; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.vjia.coolweather.R; import com.vjia.coolweather.db.CoolWeatherDB; import com.vjia.coolweather.model.City; import com.vjia.coolweather.model.County; import com.vjia.coolweather.model.Province; import com.vjia.coolweather.util.HttpCallbackListener; import com.vjia.coolweather.util.HttpUtil; import com.vjia.coolweather.util.Utility; public class ChooseAreaActivity extends Activity { public static final int LEVEL_PROVINCE = 0; public static final int LEVEL_CITY = 1; public static final int LEVEL_COUNTY = 2; private ProgressDialog progressDialog; private TextView titleText; private ListView listView; private ArrayAdapter<String> adapter; private CoolWeatherDB coolWeatherDB; private List<String> dataList = new ArrayList<String>(); private List<Province> provinceList; private List<City> cityList; private List<County> countyList; private Province selectedProvince; private City selectedCity; /** * the current level selected. */ private int currentLevel; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // switch city // get the Intent that starts current Activity Intent intent1 = getIntent(); boolean from_weather_activity = intent1.getBooleanExtra("from_weather_activity", false); // add logic to jump to WeatherActivity from ChooseAreaActivity SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if(prefs.getBoolean("city_selected", false) && !from_weather_activity){ Intent intent = new Intent(this, WeatherActivity.class); startActivity(intent); finish(); return; } // end of - add logic to jump to WeatherActivity from ChooseAreaActivity this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(R.layout.choose_area); listView = (ListView) this.findViewById(R.id.list_view); titleText = (TextView) this.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.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) { // TODO Auto-generated method stub if (currentLevel == LEVEL_PROVINCE) { selectedProvince = provinceList.get(index); queryCities(); } else if (currentLevel == LEVEL_CITY) { selectedCity = cityList.get(index); queryCounties(); } else if(currentLevel == LEVEL_COUNTY){ String countyCode = countyList.get(index).getCountyCode(); Intent intent = new Intent(ChooseAreaActivity.this, WeatherActivity.class); intent.putExtra("county_code", countyCode); startActivity(intent); finish(); } } }); queryProvinces(); } private void queryProvinces() { // TODO Auto-generated method stub 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"); } } protected void queryCities() { // TODO Auto-generated method stub cityList = coolWeatherDB.loadCities(selectedProvince.getId());// event // after // listView.setSelection(0); // ? if (cityList.size() > 0) { dataList.clear(); for (City city : cityList) { dataList.add(city.getCityName()); } adapter.notifyDataSetChanged(); listView.setSelection(0); titleText.setText(selectedProvince.getProvinceName()); currentLevel = this.LEVEL_CITY; } else { queryFromServer(selectedProvince.getProvinceCode(), "city"); } } protected void queryCounties() { // TODO Auto-generated method stub countyList = coolWeatherDB.loadCounties(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 = this.LEVEL_COUNTY; } else { queryFromServer(selectedCity.getCityCode(), "county"); } } /** * ????????????/??????????????? * * @param code * @param type */ private void queryFromServer(final String code, final String type) { String address; if (!TextUtils.isEmpty(code)) { address = "http://www.weather.com.cn/data/list3/city" + code + ".xml"; } else { address = "http://www.weather.com.cn/data/list3/city.xml"; } showProgressDialog(); HttpUtil.sendHttpRequest(address, new HttpCallbackListener() { @Override public void onFinish(String response) { // TODO Auto-generated method stub boolean result = false; if ("province".equals(type)) { result = Utility.handleProvincesResponse(coolWeatherDB, response); } else if ("city".equals(type)) { result = Utility.handleCitiesResponse(coolWeatherDB, response, selectedProvince.getId()); } else if ("county".equals(type)) { result = Utility.handleCountriesResponse(coolWeatherDB, response, selectedCity.getId()); } if (result) { // void android.app.Activity.runOnUiThread(Runnable action) // ???runOnUiThread()?????????????????? runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub closeProgressDialog(); if ("province".equals(type)) { queryProvinces(); } else if ("city".equals(type)) { queryCities(); } else if ("county".equals(type)) { queryCounties(); } } }); } } @Override public void onError(Exception e) { // TODO Auto-generated method stub // ???runOnUriThread()?????????????????? runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub closeProgressDialog(); Toast.makeText(ChooseAreaActivity.this, "???????", Toast.LENGTH_SHORT).show(); } }); } }); } private void showProgressDialog() { if (progressDialog == null) { progressDialog = new ProgressDialog(this); progressDialog.setMessage("???????....."); progressDialog.setCanceledOnTouchOutside(false); } progressDialog.show(); } private void closeProgressDialog() { if (progressDialog != null) { progressDialog.dismiss(); } } /** * ????Back????????????????????????????????????/????/???????????? */ @Override public void onBackPressed() { // TODO Auto-generated method stub // super.onBackPressed(); if (currentLevel == this.LEVEL_COUNTY) { queryCities(); } else if (currentLevel == this.LEVEL_CITY) { queryProvinces(); } else { // void android.app.Activity.finish() finish(); } } }