Android上实现一个简单的天气预报APP(六) 更新界面数据
學(xué)習(xí)參考資源:https://www.gitbook.com/book/zhangqx/mini-weather/details
前面我們已經(jīng)設(shè)置好了基本的界面,獲取了網(wǎng)絡(luò)上的天氣數(shù)據(jù)并解析出來(lái)了,接下來(lái),我們要將界面上胡亂寫的天氣數(shù)據(jù)更新為實(shí)時(shí)獲取的真實(shí)的天氣數(shù)據(jù)。
1)初始化界面
1.定義組件對(duì)應(yīng)的變量
2.編寫initView()方法
前面我們已經(jīng)在main.xml中定義了布局,這里我們可以直接通過(guò)findViewById的方法將變量與組件綁定好,并設(shè)置初值。
initView()函數(shù)如下:
void initView(){//titlecityNameT = (TextView)findViewById(R.id.title_city_name);//today weathercityT = (TextView)findViewById(R.id.todayinfo1_cityName);timeT = (TextView)findViewById(R.id.todayinfo1_updateTime);humidityT = (TextView)findViewById(R.id.todayinfo1_humidity);weekT = (TextView)findViewById(R.id.todayinfo2_week);pmDataT = (TextView)findViewById(R.id.todayinfo1_pm25);pmQualityT = (TextView)findViewById(R.id.todayinfo1_pm25status);temperatureT = (TextView)findViewById(R.id.todayinfo2_temperature);climateT = (TextView)findViewById(R.id.todayinfo2_weatherState);windT = (TextView)findViewById(R.id.todayinfo2_wind);weatherStateImg = (ImageView)findViewById(R.id.todayinfo2_weatherStatusImg);pmStateImg = (ImageView)findViewById(R.id.todayinfo1_pm25img);cityNameT.setText("N/A");cityT.setText("N/A");timeT.setText("N/A");humidityT.setText("N/A");weekT.setText("N/A");pmDataT.setText("N/A");pmQualityT.setText("N/A");temperatureT.setText("N/A");climateT.setText("N/A");windT.setText("N/A");}在onCreate中調(diào)用initView()
附:
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:id="@+id/title_relative"android:layout_width="match_parent"android:layout_height="45.0dp"android:background="#000000"><ImageViewandroid:id="@+id/title_city_manager"android:layout_height="45.0dp"android:layout_width="45.0dp"android:src="@drawable/title_city_manager"></ImageView><TextViewandroid:id="@+id/title_city_name"android:layout_height="45.0dp"android:layout_width="match_parent"android:layout_toRightOf="@id/title_city_manager"android:gravity="center_vertical"android:text="天氣預(yù)報(bào)"android:textSize="25.0sp"android:textColor="#FFFFFF"></TextView><ImageViewandroid:id="@+id/title_city_locate"android:layout_height="45.0dp"android:layout_width="45.0dp"android:layout_toLeftOf="@+id/title_city_update"android:src="@drawable/title_city_locate"></ImageView><ImageViewandroid:id="@+id/title_city_update"android:layout_height="45.0dp"android:layout_width="45.0dp"android:layout_toLeftOf="@+id/title_city_share"android:src="@drawable/title_city_update"></ImageView><ImageViewandroid:id="@+id/title_city_share"android:layout_height="45.0dp"android:layout_width="45.0dp"android:layout_alignParentRight="true"android:src="@drawable/title_city_share"></ImageView></RelativeLayout><RelativeLayoutandroid:id="@+id/today_relative"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_below="@id/title_relative"android:background="@drawable/main_background"><RelativeLayoutandroid:id="@+id/todayinfo1_relative"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:id="@+id/todayinfo1_linear1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/todayinfo1_cityName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="北京"android:textColor="#FFFFFF"android:textSize="47.0sp"/><TextViewandroid:id="@+id/todayinfo1_updateTime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="發(fā)布時(shí)間18:25"android:textColor="#FFFFFF"android:textSize="20.0sp"/><TextViewandroid:id="@+id/todayinfo1_humidity"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="濕度:57%"android:textColor="#FFFFFF"android:textSize="20.0sp"/></LinearLayout><LinearLayoutandroid:id="@+id/todayinfo1_linear2"android:layout_height="wrap_content"android:layout_width="wrap_content"android:orientation="vertical"android:layout_toLeftOf="@+id/todayinfo1_linear3"><ImageViewandroid:id="@+id/todayinfo1_pm25img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/pm25_0_50"></ImageView></LinearLayout><LinearLayoutandroid:id="@+id/todayinfo1_linear3"android:layout_height="wrap_content"android:layout_width="wrap_content"android:orientation="vertical"android:layout_alignParentRight="true"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="PM2.5"android:textColor="#FFFFFF"android:textSize="27.0sp"/><TextViewandroid:id="@+id/todayinfo1_pm25"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="220"android:textColor="#FFFFFF"android:textSize="20.0sp"/><TextViewandroid:id="@+id/todayinfo1_pm25status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="重度污染"android:textColor="#FFFFFF"android:textSize="20.0sp"/></LinearLayout></RelativeLayout><RelativeLayoutandroid:id="@+id/todayinfo2_relative"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_below="@id/todayinfo1_relative"><LinearLayoutandroid:id="@+id/todayinfo2_linear1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:orientation="vertical"><ImageViewandroid:id="@+id/todayinfo2_weatherStatusImg"android:layout_width="155dp"android:layout_height="128dp"android:src="@drawable/weather_qing"/></LinearLayout><LinearLayoutandroid:id="@+id/todayinfo2_linear2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_toRightOf="@id/todayinfo2_linear1"><TextViewandroid:id="@+id/todayinfo2_week"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="今天 星期三"android:textColor="#FFFFFF"android:textSize="27.0sp"></TextView><TextViewandroid:id="@+id/todayinfo2_temperature"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="2攝氏度~7攝氏度"android:textColor="#FFFFFF"android:textSize="20.0sp"></TextView><TextViewandroid:id="@+id/todayinfo2_weatherState"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="晴"android:textColor="#FFFFFF"android:textSize="20.0sp"></TextView><TextViewandroid:id="@+id/todayinfo2_wind"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="微風(fēng)"android:textColor="#FFFFFF"android:textSize="20.0sp"></TextView></LinearLayout></RelativeLayout></RelativeLayout> </RelativeLayout>運(yùn)行一下!2)更新數(shù)據(jù)
1.建立一個(gè)類,用于存儲(chǔ)待更新變量以及它們的setter and getter。
新建一個(gè)名為TodayWeather的java類
定義變量
右鍵-generate-setter and getter
編寫toString()
TodayWeather類完成,接下來(lái)我們要開(kāi)始賦值。
2.將我們解析的天氣數(shù)據(jù),賦值給這些我們TodayWeather中定義的變量
修改parseXML(String xmlData)函數(shù):將返回值類型改為TodayWeather,函數(shù)中定義一個(gè)TodayWeather變量,并邊解析邊賦值
private TodayWeather parseXML(String xmlData){TodayWeather todayWeather = null;int fengliCount = 0;int fengxiangCount = 0;int dateCount = 0;int highCount = 0;int lowCount = 0;int typeCount = 0;try {XmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser xmlPullParser = factory.newPullParser();xmlPullParser.setInput(new StringReader(xmlData));int eventType = xmlPullParser.getEventType();Log.d("MWeater","start parse xml");while(eventType!=xmlPullParser.END_DOCUMENT){switch (eventType){//文檔開(kāi)始位置case XmlPullParser.START_DOCUMENT:Log.d("parse","start doc");break;//標(biāo)簽元素開(kāi)始位置case XmlPullParser.START_TAG:if(xmlPullParser.getName().equals("resp")){todayWeather = new TodayWeather();}if(todayWeather!=null) {if (xmlPullParser.getName().equals("city")) {eventType = xmlPullParser.next();Log.d("city", xmlPullParser.getText());todayWeather.setCity(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("updatetime")) {eventType = xmlPullParser.next();Log.d("updatetime", xmlPullParser.getText());todayWeather.setUpdatetime(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("wendu")) {eventType = xmlPullParser.next();Log.d("wendu", xmlPullParser.getText());todayWeather.setWendu(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("fengli") && fengliCount == 0) {eventType = xmlPullParser.next();Log.d("fengli", xmlPullParser.getText());todayWeather.setFengli(xmlPullParser.getText());fengliCount++;} else if (xmlPullParser.getName().equals("shidu")) {eventType = xmlPullParser.next();Log.d("shidu", xmlPullParser.getText());todayWeather.setShidu(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("fengxiang") && fengxiangCount == 0) {eventType = xmlPullParser.next();Log.d("fengxiang", xmlPullParser.getText());todayWeather.setFengxiang(xmlPullParser.getText());fengxiangCount++;} else if (xmlPullParser.getName().equals("pm25")) {eventType = xmlPullParser.next();Log.d("pm25", xmlPullParser.getText());todayWeather.setPm25(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("quality")) {eventType = xmlPullParser.next();Log.d("quelity", xmlPullParser.getText());todayWeather.setQuality(xmlPullParser.getText());} else if (xmlPullParser.getName().equals("date") && dateCount == 0) {eventType = xmlPullParser.next();Log.d("date", xmlPullParser.getText());todayWeather.setDate(xmlPullParser.getText());dateCount++;} else if (xmlPullParser.getName().equals("high") && highCount == 0) {eventType = xmlPullParser.next();Log.d("high", xmlPullParser.getText());todayWeather.setHigh(xmlPullParser.getText());highCount++;} else if (xmlPullParser.getName().equals("low") && lowCount == 0) {eventType = xmlPullParser.next();Log.d("low", xmlPullParser.getText());todayWeather.setLow(xmlPullParser.getText());lowCount++;} else if (xmlPullParser.getName().equals("type") && typeCount == 0) {eventType = xmlPullParser.next();Log.d("type", xmlPullParser.getText());todayWeather.setType(xmlPullParser.getText());typeCount++;}}break;case XmlPullParser.END_TAG:break;}eventType=xmlPullParser.next();}}catch (Exception e){e.printStackTrace();}return todayWeather;}編寫函數(shù)updateTodayWeather(TodayWeather)更新組件 void updateTodayWeather(TodayWeather todayWeather){cityNameT.setText(todayWeather.getCity()+"天氣");cityT.setText(todayWeather.getCity());timeT.setText(todayWeather.getUpdatetime());humidityT.setText("濕度:"+todayWeather.getShidu());pmDataT.setText(todayWeather.getPm25());pmQualityT.setText(todayWeather.getQuality());weekT.setText(todayWeather.getDate());temperatureT.setText(todayWeather.getHigh()+"~"+todayWeather.getLow());climateT.setText(todayWeather.getType());windT.setText("風(fēng)力:"+todayWeather.getFengli());Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_SHORT).show();}
最后用handler啟動(dòng)更新
運(yùn)行一下!
點(diǎn)擊更新圖標(biāo)
3)根據(jù)天氣狀態(tài)更改圖片
我們將pm25值的檔次分為50以下、50-100、100-150、150-200、200-250、250-300、300以上,隨著pm25值不同更改pm25State圖片。 天氣狀態(tài)圖片也隨著實(shí)時(shí)天氣狀態(tài)更改。1.加載圖片資源
2.定義ImageView變量,并綁定組件,設(shè)置更新動(dòng)作
在MainActivity的updateTodayWeather(TodayWeather)函數(shù)中,新增代碼:updateTodayWeather void updateTodayWeather(TodayWeather todayWeather){cityNameT.setText(todayWeather.getCity()+"天氣");cityT.setText(todayWeather.getCity());timeT.setText(todayWeather.getUpdatetime());humidityT.setText("濕度:"+todayWeather.getShidu());pmDataT.setText(todayWeather.getPm25());pmQualityT.setText(todayWeather.getQuality());weekT.setText(todayWeather.getDate());temperatureT.setText(todayWeather.getHigh()+"~"+todayWeather.getLow());climateT.setText(todayWeather.getType());windT.setText("風(fēng)力:"+todayWeather.getFengli());if(todayWeather.getPm25()!=null) {int pm25 = Integer.parseInt(todayWeather.getPm25());if (pm25 <= 50) {PM25Img.setImageResource(R.drawable.biz_plugin_weather_0_50);} else if (pm25 >= 51 && pm25 <= 100) {PM25Img.setImageResource(R.drawable.biz_plugin_weather_51_100);} else if (pm25 >= 101 && pm25 <= 150) {PM25Img.setImageResource(R.drawable.biz_plugin_weather_101_150);} else if (pm25 >= 151 && pm25 <= 200) {PM25Img.setImageResource(R.drawable.biz_plugin_weather_151_200);} else if (pm25 >= 201 && pm25 <= 300) {PM25Img.setImageResource(R.drawable.biz_plugin_weather_201_300);}}if(todayWeather.getType()!=null) {Log.d("type", todayWeather.getType());switch (todayWeather.getType()) {case "晴":weatherImg.setImageResource(R.drawable.biz_plugin_weather_qing);break;case "陰":weatherImg.setImageResource(R.drawable.biz_plugin_weather_yin);break;case "霧":weatherImg.setImageResource(R.drawable.biz_plugin_weather_wu);break;case "多云":weatherImg.setImageResource(R.drawable.biz_plugin_weather_duoyun);break;case "小雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_xiaoyu);break;case "中雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhongyu);break;case "大雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_dayu);break;case "陣雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhenyu);break;case "雷陣雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_leizhenyu);break;case "雷陣雨加暴":weatherImg.setImageResource(R.drawable.biz_plugin_weather_leizhenyubingbao);break;case "暴雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_baoyu);break;case "大暴雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_dabaoyu);break;case "特大暴雨":weatherImg.setImageResource(R.drawable.biz_plugin_weather_tedabaoyu);break;case "陣雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhenxue);break;case "暴雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_baoxue);break;case "大雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_daxue);break;case "小雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_xiaoxue);break;case "雨夾雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_yujiaxue);break;case "中雪":weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhongxue);break;case "沙塵暴":weatherImg.setImageResource(R.drawable.biz_plugin_weather_shachenbao);break;default:break;}}Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_SHORT).show();} 運(yùn)行一下!
總結(jié)
以上是生活随笔為你收集整理的Android上实现一个简单的天气预报APP(六) 更新界面数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【办公-excel】VBA 选择文件、文
- 下一篇: java升序降序排列desc,TreeM