標籤:
繼續昨天的學習。
昨天我們根據取得的天氣資料建立了一個視圖用來顯示各項內容,那麼今天我們就把資料顯示出來吧!!!
這裡我們要把資料和視圖聯絡起來,那麼就用到了適配器-Adapter,Android給我們提供了很多Adapter,這裡我們用到了BaseAdapter。
BaseAdapter(1)
右鍵點擊src/com.demo.weather,選擇 New > Class,按照填寫:
選擇[Finish]後,我們就建立了一個BaseAdapter的子類,開啟 WeatherAdapter.java這個檔案,來解釋一下這些代碼,
public class WeatherAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return 0; } @Override public Object getItem( int arg0 ) { // TODO Auto-generated method stub return null; } @Override public long getItemId( int arg0 ) { // TODO Auto-generated method stub return 0; } @Override public View getView( int arg0, View arg1, ViewGroup arg2 ) { // TODO Auto-generated method stub return null; }}
public int getCount()
這個方法返回ListView有幾條資料,
public Object getItem( int arg0 )
這個方法返回當前行的資料,
public long getItemId( int arg0 )
這個方法返回當前行的id,
public View getView( int arg0, View arg1, ViewGroup arg2 )
這個方法返回當前行的視圖,
那麼究竟怎樣把資料和視圖關聯起來了。
首先,我們需要把取得的資料轉換為代碼可以方便操作的形式。把json資料轉換為JavaBean,我們需要用到一個gson庫,可以在這裡下載。 http://code.google.com/p/google-gson/
把下載回來的gson-v2.jar放到libs檔案夾,然後開始我們辛苦的工作吧,將是一大段代碼。
Gson
建立一個package包,命名為com.demo.weather.bean,在這個包下面添加4個類,BaiduData、ResultsBean、IndexBean、WeatherDataBean,內容分別是:
public class BaiduData{ private int error; private String status; private String date; private List<ResultsBean> results; public int getError() { return error; } public void setError( int error ) { this.error = error; } public String getStatus() { return status; } public void setStatus( String status ) { this.status = status; } public String getDate() { return date; } public void setDate( String date ) { this.date = date; } public List<ResultsBean> getResults() { return results; } public void setResults( List<ResultsBean> results ) { this.results = results; }}
public class ResultsBean{ private String currentCity; private String pm25; private List<IndexBean> index; private List<WeatherDataBean> weather_data; public String getCurrentCity() { return currentCity; } public void setCurrentCity( String currentCity ) { this.currentCity = currentCity; } public String getPm25() { return pm25; } public void setPm25( String pm25 ) { this.pm25 = pm25; } public List<IndexBean> getIndex() { return index; } public void setIndex( List<IndexBean> index ) { this.index = index; } public List<WeatherDataBean> getWeather_data() { return weather_data; } public void setWeather_data( List<WeatherDataBean> weather_data ) { this.weather_data = weather_data; }}
public class IndexBean{ private String title; private String zs; private String tipt; private String des; public String getTitle() { return title; } public void setTitle( String title ) { this.title = title; } public String getZs() { return zs; } public void setZs( String zs ) { this.zs = zs; } public String getTipt() { return tipt; } public void setTipt( String tipt ) { this.tipt = tipt; } public String getDes() { return des; } public void setDes( String des ) { this.des = des; }}
public class WeatherDataBean{ private String date; private String dayPictureUrl; private String nightPictureUrl; private String weather; private String wind; private String temperature; public String getDate() { return date; } public void setDate( String date ) { this.date = date; } public String getDayPictureUrl() { return dayPictureUrl; } public void setDayPictureUrl( String dayPictureUrl ) { this.dayPictureUrl = dayPictureUrl; } public String getNightPictureUrl() { return nightPictureUrl; } public void setNightPictureUrl( String nightPictureUrl ) { this.nightPictureUrl = nightPictureUrl; } public String getWeather() { return weather; } public void setWeather( String weather ) { this.weather = weather; } public String getWind() { return wind; } public void setWind( String wind ) { this.wind = wind; } public String getTemperature() { return temperature; } public void setTemperature( String temperature ) { this.temperature = temperature; }}
這4個JavaBean承載了json格式的字串轉換後的對象。
然後,開啟MainActivity.java檔案,這個檔案還有一個小紅叉,那是我們之前刪掉了一個文本後沒有及時修改代碼造成的後果。不用客氣,把它刪掉。
修改http.send方法為以下內容:
http.send( HttpMethod.GET, "http://api.map.baidu.com/telematics/v3/weather", params, new RequestCallBack<String>() { @Override public void onSuccess( ResponseInfo<String> responseInfo ) { String weather = responseInfo.result; Gson gson = new Gson(); BaiduData data = gson.fromJson( weather, BaiduData.class ); Log.v( "onFailure", data.toString() ); } @Override public void onFailure( HttpException arg0, String arg1 ) { Log.v( "onFailure", arg1 ); } } );
到這裡,先搞一段落,來解釋一下這麼多的代碼。
雖然代碼很多,但是都很簡單,BaiduData、ResultsBean、IndexBean、WeatherDataBean這4個類是根據gson的要求,結合百度天氣API的返回資料做成的標準JavaBean。
Gson gson = new Gson(); BaiduData data = gson.fromJson( weather, BaiduData.class );
這兩行是核心功能,把Json格式的字串轉換為了我們做成的BaiduData這個JavaBean。
BaseAdapter(2)
既然已經把資料格式化為JavaBean了,剩下的工作就簡單多了。
修改WeatherAdapter使得資料和視圖關聯,
public class WeatherAdapter extends BaseAdapter{ private List<WeatherDataBean> weathers; private LayoutInflater inflater; private BitmapUtils bitmapUtils; public WeatherAdapter( Context context, List<WeatherDataBean> weathers ) { this.weathers = weathers; inflater = LayoutInflater.from( context ); bitmapUtils = new BitmapUtils( context ); } @Override public int getCount() { return weathers.size(); } @Override public Object getItem( int position ) { return weathers.get( position ); } @Override public long getItemId( int position ) { return position; } @Override public View getView( int position, View convertView, ViewGroup parent ) { convertView = inflater.inflate( R.layout.item_weather, null ); TextView txtDate = (TextView)convertView.findViewById( R.id.item_date ); TextView txtWeather = (TextView)convertView.findViewById( R.id.item_weather ); TextView txtWind = (TextView)convertView.findViewById( R.id.item_wind ); TextView txtTemperature = (TextView)convertView.findViewById( R.id.item_temperature ); ImageView imgTemperature = (ImageView)convertView.findViewById( R.id.item_picture ); WeatherDataBean bean = (WeatherDataBean)getItem( position ); txtDate.setText( bean.getDate() ); txtWeather.setText( bean.getWeather() ); txtWind.setText( bean.getWind() ); txtTemperature.setText( bean.getTemperature() ); bitmapUtils.display( imgTemperature, bean.getDayPictureUrl() ); return convertView; }}
這裡我們使用了LayoutInflater來載入昨天做成的item_weather介面,使用了BitmapUtils來載入網狀圖片。雖然是大段代碼,但是道理很簡單,把對應的資料顯示到對應的項目。
然後,在MainActivity.java裡添加對ListView以及我們之前建好的WeatherAdapter類的引用,並且將ListView、WeatherAdapter、BaiduData三個對象關聯起來,
public class WeatherAdapter extends BaseAdapter{ private List<WeatherDataBean> weathers; private LayoutInflater inflater; private BitmapUtils bitmapUtils; public WeatherAdapter( Context context, List<WeatherDataBean> weathers ) { this.weathers = weathers; inflater = LayoutInflater.from( context ); bitmapUtils = new BitmapUtils( context ); } @Override public int getCount() { return weathers.size(); } @Override public Object getItem( int position ) { return weathers.get( position ); } @Override public long getItemId( int position ) { return position; } @Override public View getView( int position, View convertView, ViewGroup parent ) { convertView = inflater.inflate( R.layout.item_weather, null ); TextView txtDate = (TextView)convertView.findViewById( R.id.item_date ); TextView txtWeather = (TextView)convertView.findViewById( R.id.item_weather ); TextView txtWind = (TextView)convertView.findViewById( R.id.item_wind ); TextView txtTemperature = (TextView)convertView.findViewById( R.id.item_temperature ); ImageView imgTemperature = (ImageView)convertView.findViewById( R.id.item_picture ); WeatherDataBean bean = (WeatherDataBean)getItem( position ); txtDate.setText( bean.getDate() ); txtWeather.setText( bean.getWeather() ); txtWind.setText( bean.getWind() ); txtTemperature.setText( bean.getTemperature() ); bitmapUtils.display( imgTemperature, bean.getDayPictureUrl() ); return convertView; }}
打完收工。
最終的效果如下:
同志們,很有成就感了吧!!!
不要得意,這隻是把資料簡單的顯示出來而已,並且只能是帝都北京的資料,如果你不在北京,這根本對你一點用處都沒有啊。
嗯嗯,今天就到這兒吧,已經足夠多了,好學的你肯定已經在尋找更多關於Adapter、ListView的知識了,這裡我就不做介紹了,建議大家經常百度。
休閑一下,說點廢話。
本人從事IT行業10餘年,從最初級的菜鳥程式員開始,一步一步成長,見過形形色色的程式員,從個人的角度把程式員分成這麼幾個層級:
1. 無腦
這個指的是一遇到問題就問別人的同志,自己也不思考,也不求上進,混一天算一天。這種類型是極少數的,我也只是聽說過,都是傳說中的人物。
2. 菜鳥
這種類型的程式員非常多,通常是接觸技術時間不長的朋友。這部分通常是沒有網路就不能工作了,經常在論壇裡尋找資料,下載別人代碼拿來使用,鍵盤上的Ctrl、C、V鍵都磨光了。
3. 小牛
這種類型多見於技術負責人、技術總監這樣的職位,要想做到小牛這一步,需要對技術有比較深的瞭解,能夠從無到有進行設計的。在各種開源社區,這部分人通常會比較活躍,菜鳥們大都使用小牛們的勞動成果。
4. 大牛
這種類型的人已經非常少了,我只見過那麼幾位,都是各大公司CTO之類的。說說其中一位,他從無到有設計了公司的雲端運算方案,從硬體的基礎設施到軟體層面,再到使用者層面,這位大牛都有深入的掌握。
5. 傳說
鄙人只能在各種新聞、書籍中才能見到了。個人認為這部分人的技術已經不是我們可以評判的了,他們的思想、影響力根本不是我等可以企及的。
各位親,你是哪個層級的?
附件是本次的工程檔案,點擊下載。
此系列文章系本人原創,如需轉載,請註明出處 www.liuzhibang.cn
10天學安卓-第四天