Back to project page MyWeather.
The source code is released under:
Apache License
If you think the Android project MyWeather 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 activity; /*from w w w . j av a 2 s . c o m*/ import java.util.ArrayList; import java.util.List; import model.City; import model.County; import model.Province; import util.HttpCallbackListener; import util.HttpUtil; import util.Utility; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 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.myweather.app.R; import db.MyWeatherDB; 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 MyWeatherDB myWeatherDB; private List<String> dataList = new ArrayList<String>(); /** * Province List */ private List<Province> provinceList; /** * City List */ private List<City> cityList; /** * County List */ private List<County> countyList; /** * selected province */ private Province selectedProvince; /** * selected city */ private City selectedCity; /** * selected level */ private int currentLevel; private boolean isFromWeatherActivity; @SuppressLint("InlinedApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); isFromWeatherActivity = getIntent().getBooleanExtra("from_weather_activity", false); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if(prefs.getBoolean("city_selected", false) && !isFromWeatherActivity){ Intent intent = new Intent(this, WeatherActivity.class); startActivity(intent); finish(); return; } requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.choose_area); final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container); swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // TODO Auto-generated method stub new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub swipeRefreshLayout.setRefreshing(false); } }, 5000); } }); swipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); 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); myWeatherDB = MyWeatherDB.getInstance(this); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub if (currentLevel == LEVEL_PROVINCE){ selectedProvince = provinceList.get(position); queryCities(); }else if(currentLevel == LEVEL_CITY){ selectedCity = cityList.get(position); queryCounties(); }else if(currentLevel == LEVEL_COUNTY){ String countyCode = countyList.get(position).getCountyCode(); Intent intent = new Intent(ChooseAreaActivity.this, WeatherActivity.class); intent.putExtra("county_code", countyCode); startActivity(intent); finish(); } } }); queryProvinces(); } /** * query province */ private void queryProvinces(){ provinceList = myWeatherDB.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"); } } /** * query city */ private void queryCities(){ cityList = myWeatherDB.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"); } } /** * query county */ private void queryCounties(){ countyList = myWeatherDB.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 = LEVEL_COUNTY; } else{ queryFromServer(selectedCity.getCityCode(), "county"); } } /** * query from server * @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(myWeatherDB, response); }else if("city".equals(type)){ result = Utility.handleCitiesResponse(myWeatherDB, response, selectedProvince.getId()); }else if("county".equals(type)){ result = Utility.handleCountiesResponse(myWeatherDB, response, selectedCity.getId()); } if(result){ runOnUiThread(new Runnable() { public void run() { 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 runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub closeProgressDialog(); Toast.makeText(ChooseAreaActivity.this, "????", Toast.LENGTH_SHORT).show(); } }); } }); } /** * show dialog */ private void showProgressDialog(){ if(progressDialog == null){ progressDialog = new ProgressDialog(this); progressDialog.setMessage("????..."); progressDialog.setCanceledOnTouchOutside(false); } progressDialog.show(); } /** * close dialog */ private void closeProgressDialog(){ if(progressDialog != null){ progressDialog.dismiss(); } } @Override public void onBackPressed() { // TODO Auto-generated method stub if(currentLevel == LEVEL_COUNTY){ queryCities(); }else if(currentLevel == LEVEL_CITY){ queryProvinces(); }else{ if(isFromWeatherActivity){ Intent intent = new Intent(this, WeatherActivity.class); startActivity(intent); } finish(); } } }