From a54594610f6ef006327533dc7d5e1007d40c9dd9 Mon Sep 17 00:00:00 2001 From: tony Date: Sat, 14 May 2016 20:35:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=81=8D=E5=8E=86=E7=9C=81?= =?UTF-8?q?=E5=B8=82=E5=8E=BF=E4=B8=89=E7=BA=A7=E5=88=97=E8=A1=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/ChooseAreaActivity.java | 199 ++++++++++++++++++ .../java/com/app/myweather/model/County.java | 4 +- .../com/app/myweather/model/MyWeatherDB.java | 19 +- .../myweather/util/HttpCallbackListener.java | 9 + .../java/com/app/myweather/util/HttpUtil.java | 47 +++++ .../java/com/app/myweather/util/Utility.java | 63 ++++++ app/src/main/AndroidManifest.xml | 12 +- app/src/main/res/layout/choose_area.xml | 26 +++ 8 files changed, 370 insertions(+), 9 deletions(-) create mode 100644 app/src/androidTest/java/com/app/myweather/activity/ChooseAreaActivity.java create mode 100644 app/src/androidTest/java/com/app/myweather/util/HttpCallbackListener.java create mode 100644 app/src/androidTest/java/com/app/myweather/util/HttpUtil.java create mode 100644 app/src/androidTest/java/com/app/myweather/util/Utility.java create mode 100644 app/src/main/res/layout/choose_area.xml diff --git a/app/src/androidTest/java/com/app/myweather/activity/ChooseAreaActivity.java b/app/src/androidTest/java/com/app/myweather/activity/ChooseAreaActivity.java new file mode 100644 index 0000000..388a521 --- /dev/null +++ b/app/src/androidTest/java/com/app/myweather/activity/ChooseAreaActivity.java @@ -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 adapter; + private MyWeatherDB myWeatherDB; + private List dataList = new ArrayList(); + private List provinceList; + private List cityList; + private List 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(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(); + } + } + +} + + diff --git a/app/src/androidTest/java/com/app/myweather/model/County.java b/app/src/androidTest/java/com/app/myweather/model/County.java index 678d12c..e879029 100644 --- a/app/src/androidTest/java/com/app/myweather/model/County.java +++ b/app/src/androidTest/java/com/app/myweather/model/County.java @@ -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){ @@ -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(){ diff --git a/app/src/androidTest/java/com/app/myweather/model/MyWeatherDB.java b/app/src/androidTest/java/com/app/myweather/model/MyWeatherDB.java index abc9589..853239f 100644 --- a/app/src/androidTest/java/com/app/myweather/model/MyWeatherDB.java +++ b/app/src/androidTest/java/com/app/myweather/model/MyWeatherDB.java @@ -67,7 +67,7 @@ public void saveCity(City city){ ,values); } } - public List(City) loadCities(int provinceId){ + public List loadCities(int provinceId){ List list=new ArrayList(); Cursor cursor=db.query("City",null,"province_id=?",new String[]{String.valueOf(provinceId)},null,null,null); if (cursor.moveToFirst()){ @@ -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 loadCounties(int cityId){ List list=new ArrayList(); - 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; } } diff --git a/app/src/androidTest/java/com/app/myweather/util/HttpCallbackListener.java b/app/src/androidTest/java/com/app/myweather/util/HttpCallbackListener.java new file mode 100644 index 0000000..7046c6d --- /dev/null +++ b/app/src/androidTest/java/com/app/myweather/util/HttpCallbackListener.java @@ -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); +} diff --git a/app/src/androidTest/java/com/app/myweather/util/HttpUtil.java b/app/src/androidTest/java/com/app/myweather/util/HttpUtil.java new file mode 100644 index 0000000..258cc18 --- /dev/null +++ b/app/src/androidTest/java/com/app/myweather/util/HttpUtil.java @@ -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(); + } +} diff --git a/app/src/androidTest/java/com/app/myweather/util/Utility.java b/app/src/androidTest/java/com/app/myweather/util/Utility.java new file mode 100644 index 0000000..b326808 --- /dev/null +++ b/app/src/androidTest/java/com/app/myweather/util/Utility.java @@ -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; + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6228d4..59c77f8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,22 @@ + + android:theme="@style/AppTheme"> + + + + + + > + diff --git a/app/src/main/res/layout/choose_area.xml b/app/src/main/res/layout/choose_area.xml new file mode 100644 index 0000000..bf455ca --- /dev/null +++ b/app/src/main/res/layout/choose_area.xml @@ -0,0 +1,26 @@ + + + + + + + + + + +