Skip to content

Commit

Permalink
完成遍历省市县三级列表功能
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed May 14, 2016
1 parent 927a022 commit a545946
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.app.myweather.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
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.app.myweather.R;
import com.app.myweather.model.City;
import com.app.myweather.model.County;
import com.app.myweather.model.MyWeatherDB;
import com.app.myweather.model.Province;
import com.app.myweather.util.HttpCallbackListener;
import com.app.myweather.util.HttpUtil;
import com.app.myweather.util.Utility;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2016/5/14.
*/
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>();
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, dataList);
listView.setAdapter(adapter);
myWeatherDB = MyWeatherDB.getInstance(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long arg3) {

if (currentLevel == LEVEL_PROVINCE) {
selectedProvince = provinceList.get(index);
queryCities();
} else if (currentLevel == LEVEL_CITY) {
selectedCity = cityList.get(index);
queryCounties();
}
}
});
queryProvinces();
}

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("Chinese");
currentLevel = LEVEL_PROVINCE;
} else {
queryFromServer(null, "provicne");
}
}

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");
}
}

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");
}
}

private void queryFromServer(final String code, final String type) {
String address;
if (!TextUtils.isEmpty(code)) {
address = "http.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) {
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() {
@Override
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) {
runOnUiThread(new Runnable() {
@Override
public void run() {
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();
}
}

@Override
public void onBackPressed() {
if (currentLevel == LEVEL_COUNTY) {
queryCities();
} else if (currentLevel == LEVEL_CITY) {
queryProvinces();
} else {
finish();
}
}

}


4 changes: 2 additions & 2 deletions app/src/androidTest/java/com/app/myweather/model/County.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class County {
private String countyName;
private String countyCode;
private int cityId;
private int getId(){
public int getId(){
return id;
}
public void setId(int id){
Expand All @@ -17,7 +17,7 @@ public void setId(int id){
public String getCountyName(){
return countyName=countyName;
}
private void setCountyName(String countyName){
public void setCountyName(String countyName){
this.countyName=countyName;
}
public String getCountyCode(){
Expand Down
19 changes: 13 additions & 6 deletions app/src/androidTest/java/com/app/myweather/model/MyWeatherDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void saveCity(City city){
,values);
}
}
public List(City) loadCities(int provinceId){
public List<City> loadCities(int provinceId){
List<City> list=new ArrayList<City>();
Cursor cursor=db.query("City",null,"province_id=?",new String[]{String.valueOf(provinceId)},null,null,null);
if (cursor.moveToFirst()){
Expand All @@ -90,20 +90,27 @@ public void saveCounty(County county){
ContentValues values=new ContentValues();
values.put("county_name",county.getCountyName());
values.put("county_code",county.getCountyCode());
values.put(("city_id",county.getCityId()));
values.put("city_id",county.getCityId());
db.insert("County",null,values);
}
}
public List<County> loadCounties(int cityId){
List<County> list=new ArrayList<County>();
Cursor cursor=db.query("County",null,"city_id=?",new String[]{String.valueOf(cityId)},null,null,null);
Cursor cursor=db.query("County",null,"city_id=?",new String[] {String.valueOf(cityId)},null,null,null);
if (cursor.moveToFirst()){
do {
County county=new County();
county.setId(cursor.getInt("cursaor.getColumnIndex("id")));
county.s
}
county.setId(cursor.getInt(cursor.getColumnIndex("id")));
county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
county.setCityId(cityId);
list.add(county);
}while(cursor.moveToNext());
}
if (cursor !=null){
cursor.close();
}
return list;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.app.myweather.util;

/**
* Created by Administrator on 2016/5/14.
*/
public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
47 changes: 47 additions & 0 deletions app/src/androidTest/java/com/app/myweather/util/HttpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.app.myweather.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

/**
* Created by Administrator on 2016/5/14.
*/
public class HttpUtil {
public static void sendHttpRequest(final String address,final HttpCallbackListener listener){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
try{
URL url=new URL(address);
connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while ((line=reader.readLine()) !=null){
response.append(line);
}
if (listener !=null){
listener.onFinish(response.toString());
}
}catch(Exception e){
if (listener !=null){
listener.onError(e);
}
}finally {
if (connection !=null){
connection.disconnect();
}
}
}
}).start();
}
}
63 changes: 63 additions & 0 deletions app/src/androidTest/java/com/app/myweather/util/Utility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.app.myweather.util;

import android.text.TextUtils;

import com.app.myweather.model.City;
import com.app.myweather.model.County;
import com.app.myweather.model.MyWeatherDB;
import com.app.myweather.model.Province;

/**
* Created by Administrator on 2016/5/14.
*/
public class Utility {
public synchronized static boolean handleProvincesResponse(MyWeatherDB myWeatherDB,String response){
if (!TextUtils.isEmpty(response)){
String[] allProvinces =response.split(",");
if (allProvinces !=null && allProvinces.length>0){
for (String p:allProvinces){
String[] array=p.split("\\|");
Province province=new Province();
province.setProvinceCode(array[0]);
province.setProvinceName(array[1]);
myWeatherDB.saveProvince(province);
}
return true;

}
}
return false;
}
public static boolean handleCitiesResponse(MyWeatherDB myWeatherDB,String response,int provinceId){
if (!TextUtils.isEmpty(response)){
String[] allCities=response.split(",");
if (allCities !=null && allCities.length>0){
for (String c:allCities){
String[] array=c.split("\\|");
City city=new City();
city.setCityCode(array[0]);
city.setCityName(array[1]);
city.setProvinceId(provinceId);
myWeatherDB.saveCity(city);
}
return true;
}
}
return false;
}
public static boolean handleCountiesResponse(MyWeatherDB myWeatherDB,String response,int cityId){
if (!TextUtils.isEmpty(response)){
String[] allCounties =response.split(",");
if (allCounties !=null && allCounties.length >0){
for (String c:allCounties){
String[] array=c.split("\\|");
County county=new County();
county.setCountyCode(array[0]);
county.setCountyName(array[1]);
county.setCityId(cityId);
myWeatherDB.saveCounty(county);
}return true;
}
}return false;
}
}
Loading

0 comments on commit a545946

Please sign in to comment.