public class MainActivity extends Activity {
//AppCompatActivity
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> dataLits = new ArrayList<String>();
/**
* 省列表
*/
private List<Province> provinceList;
private List<City> cityList;
private List<County> countyList;
/**
* 选中的省份
*/
private Province selectedProvince;
private City selectedCity;
/**
* 当前选中的级别
*/
private int currentLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//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<String>(this,android.R.layout.simple_list_item_1,dataLits);
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 = provinceList.get(position);
queryCities();
} else if (currentLevel == LEVEL_CITY) {
selectedCity = cityList.get(position);
queryCounties();
}
}
});
queryProvinces(); } /**
* 查询全国所有的省,优先从数据库中查询,如果没有查询到再到服务气中查询
*/
private void queryProvinces() {
provinceList = coolWeatherDB.loadProvince();
if(provinceList.size()>0) {
Toast.makeText(this,"从数据库中读取",Toast.LENGTH_LONG).show();
dataLits.clear();
for(Province province:provinceList) {
dataLits.add(province.getProvinceName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText("中国");
currentLevel = LEVEL_PROVINCE;
} else {
queryFromServer(null, "province");
Toast.makeText(this,"从服务器中读取",Toast.LENGTH_LONG).show();
}
}
private void queryCities() {
cityList = coolWeatherDB.loadCities(selectedProvince.getId());
if(cityList.size()>0) {
Toast.makeText(this, "数据库", Toast.LENGTH_LONG).show();
dataLits.clear();
for(City city : cityList){
dataLits.add(city.getCityName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText(selectedProvince.getProvinceName());
currentLevel = LEVEL_CITY;
}else {
queryFromServer(selectedProvince.getProvinceCode(),"city");
}
}
private void queryCounties() {
countyList = coolWeatherDB.loadConuties(selectedCity.getId());
if(countyList.size()>0) {
Toast.makeText(this, "数据库", Toast.LENGTH_LONG).show();
dataLits.clear();
for(County county : countyList){
dataLits.add(county.getCountyName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText(selectedCity.getCityName());
currentLevel = LEVEL_COUNTY;
}else {
queryFromServer(selectedCity.getCityCode(),"county");
}
} private void queryFromServer(final String code, final String type) { String address;
if(!TextUtils.isEmpty(code)){
address = "http://www.weather.com.cn/data/list3/city19.xml";
}else {
address = "http://www.weather.com.cn/data/list3/city.xml";
}
//showProgressDialog();
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
@Override
public void onFinish(String response) {
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.handleCountiesResponse(coolWeatherDB, response,
selectedCity.getId());
}
if (result) {
//通过runOnUiThread方法回到主线程处理逻辑上
runOnUiThread(new Runnable() {
@Override
public void run() {
if (type.equals("province")) {
queryProvinces();
} else if (type.equals("city")) {
queryCities();
} else if (type.equals("county")) {
queryCounties();
}
}
});
}
}
@Override
public void onError(Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
closeProgressDialog();
Toast.makeText(MainActivity.this,"加载失败,服务器挂了",Toast.LENGTH_LONG).show();
}
});
}
});
} private void showProgressDialog() {
if(progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("正在加载中····");
progressDialog.setCanceledOnTouchOutside(false);
}
progressDialog.show();
} @Override
public void onBackPressed() {
super.onBackPressed();
if(currentLevel==LEVEL_COUNTY){
queryCities();
}else if(currentLevel==LEVEL_CITY){
queryProvinces();
}else {
finish();
}
} private void closeProgressDialog(){
if(progressDialog!=null){[code=java][code=java][/code][/code]
progressDialog.dismiss();
}
}
}
//AppCompatActivity
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> dataLits = new ArrayList<String>();
/**
* 省列表
*/
private List<Province> provinceList;
private List<City> cityList;
private List<County> countyList;
/**
* 选中的省份
*/
private Province selectedProvince;
private City selectedCity;
/**
* 当前选中的级别
*/
private int currentLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//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<String>(this,android.R.layout.simple_list_item_1,dataLits);
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 = provinceList.get(position);
queryCities();
} else if (currentLevel == LEVEL_CITY) {
selectedCity = cityList.get(position);
queryCounties();
}
}
});
queryProvinces(); } /**
* 查询全国所有的省,优先从数据库中查询,如果没有查询到再到服务气中查询
*/
private void queryProvinces() {
provinceList = coolWeatherDB.loadProvince();
if(provinceList.size()>0) {
Toast.makeText(this,"从数据库中读取",Toast.LENGTH_LONG).show();
dataLits.clear();
for(Province province:provinceList) {
dataLits.add(province.getProvinceName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText("中国");
currentLevel = LEVEL_PROVINCE;
} else {
queryFromServer(null, "province");
Toast.makeText(this,"从服务器中读取",Toast.LENGTH_LONG).show();
}
}
private void queryCities() {
cityList = coolWeatherDB.loadCities(selectedProvince.getId());
if(cityList.size()>0) {
Toast.makeText(this, "数据库", Toast.LENGTH_LONG).show();
dataLits.clear();
for(City city : cityList){
dataLits.add(city.getCityName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText(selectedProvince.getProvinceName());
currentLevel = LEVEL_CITY;
}else {
queryFromServer(selectedProvince.getProvinceCode(),"city");
}
}
private void queryCounties() {
countyList = coolWeatherDB.loadConuties(selectedCity.getId());
if(countyList.size()>0) {
Toast.makeText(this, "数据库", Toast.LENGTH_LONG).show();
dataLits.clear();
for(County county : countyList){
dataLits.add(county.getCountyName());
}
adapter.notifyDataSetChanged();
listView.setSelection(0);
titleText.setText(selectedCity.getCityName());
currentLevel = LEVEL_COUNTY;
}else {
queryFromServer(selectedCity.getCityCode(),"county");
}
} private void queryFromServer(final String code, final String type) { String address;
if(!TextUtils.isEmpty(code)){
address = "http://www.weather.com.cn/data/list3/city19.xml";
}else {
address = "http://www.weather.com.cn/data/list3/city.xml";
}
//showProgressDialog();
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
@Override
public void onFinish(String response) {
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.handleCountiesResponse(coolWeatherDB, response,
selectedCity.getId());
}
if (result) {
//通过runOnUiThread方法回到主线程处理逻辑上
runOnUiThread(new Runnable() {
@Override
public void run() {
if (type.equals("province")) {
queryProvinces();
} else if (type.equals("city")) {
queryCities();
} else if (type.equals("county")) {
queryCounties();
}
}
});
}
}
@Override
public void onError(Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
closeProgressDialog();
Toast.makeText(MainActivity.this,"加载失败,服务器挂了",Toast.LENGTH_LONG).show();
}
});
}
});
} private void showProgressDialog() {
if(progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("正在加载中····");
progressDialog.setCanceledOnTouchOutside(false);
}
progressDialog.show();
} @Override
public void onBackPressed() {
super.onBackPressed();
if(currentLevel==LEVEL_COUNTY){
queryCities();
}else if(currentLevel==LEVEL_CITY){
queryProvinces();
}else {
finish();
}
} private void closeProgressDialog(){
if(progressDialog!=null){[code=java][code=java][/code][/code]
progressDialog.dismiss();
}
}
}
解决方案 »
- 电脑做服务器,手机端做客户。在模拟器上能连接,但安装到手机上就连接不上
- 为什么contentprovider中的getType需要返回MIME呢
- 我做了个android视频监控传输程序,运行了一会儿死机了,怎么样才能找到问题所在?
- android的ndk-gdb调试问题
- FilterInputStream skip 的使用
- 求助 学习路线的问题。。三大框架或者Android???
- json解析出现个问题
- 请问从0开始自学Andriod的APP需要多久?
- 关于不继承TabActivity使用TabHost的问题
- Android studio 运行报错
- 闹钟程序休眠后不再提示。大家进来讨论一下吧。
- Gallery2图库主界面修改
很明显,请求省级以下数据时没有带上省的code参数。那么原因基本就是路径写错了。
查看了一下电子书的源码。
if(!TextUtils.isEmpty(code)){
address = "http://www.weather.com.cn/data/list3/city19.xml";
}else {
address = "http://www.weather.com.cn/data/list3/"+code+"city.xml";
}
应该可以解决问题了。