项目源码在Github上面有Github项目源码
在这里节选一下两个主要的Activity中的源码
ChooseAreaActivity.java
package com.example.chencong.coolweather.activity;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chencong.coolweather.R;
import com.example.chencong.coolweather.db.CoolWeatherDB;
import com.example.chencong.coolweather.model.City;
import com.example.chencong.coolweather.model.County;
import com.example.chencong.coolweather.model.Province;
import com.example.chencong.coolweather.util.HttpCallbackListener;
import com.example.chencong.coolweather.util.HttpUtil;
import com.example.chencong.coolweather.util.Utility;
import java.util.ArrayList;
import java.util.List;
/**
* Created by chencong on 2016/7/13.
*/
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;
//当前选中的级别
private int currentLevel;
/*
* 是否从WeatherActivity中跳转过来*/
private boolean isFromWeatherActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFromWeatherActivity = getIntent().getBooleanExtra("from_weather_activity",false);
//从ChooseAreaActivity跳转到WeatherActivity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//对已经选择了城市并且不是从WeatherActivity跳转过来,才会直接跳转到WeatherActivity
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);
listView = (L