10天學安卓-第四天

繼續昨天的學習。java

昨天咱們根據取得的天氣數據新建了一個視圖用來顯示各項內容,那麼今天咱們就把數據顯示出來吧!!!程序員

這裏咱們要把數據和視圖聯繫起來,那麼就用到了適配器-Adapter,Android給咱們提供了不少Adapter,這裏咱們用到了BaseAdapter。json

 

BaseAdapter(1)

右鍵點擊src/com.demo.weather,選擇 New > Class,按照下圖填寫:api

QQ截圖20140921212540

選擇[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有幾條數據,ide

 

public Object getItem( int arg0 )

這個方法返回當前行的數據,學習

 

public long getItemId( int arg0 )

這個方法返回當前行的id,this

 

public View getView( int arg0, View arg1, ViewGroup arg2 )

這個方法返回當前行的視圖,google

那麼究竟怎樣把數據和視圖關聯起來了。雲計算

 

首先,咱們須要把取得的數據轉換爲代碼能夠方便操做的形式。把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 MainActivity extends Activity
{
    @ViewInject( R.id.weather_list )
    private ListView lstWeather;

    private WeatherAdapter adapter;
    private BaiduData data;

    private List<WeatherDataBean> datas;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        ViewUtils.inject( this );

        HttpUtils http = new HttpUtils();

        datas = new ArrayList<WeatherDataBean>();
        adapter = new WeatherAdapter( getApplicationContext(), datas );
        lstWeather.setAdapter( adapter );

        RequestParams params = new RequestParams();
        params.addQueryStringParameter( "location", "北京" );
        params.addQueryStringParameter( "output", "json" );
        params.addQueryStringParameter( "ak", "YknGmxIoPugT7YrNrG955YLS" );

        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();
                data = gson.fromJson( weather, BaiduData.class );

                datas.clear();
                datas.addAll( data.getResults().get( 0 ).getWeather_data() );
                adapter.notifyDataSetChanged();

                Log.v( "onSuccess", data.toString() );
            }

            @Override
            public void onFailure( HttpException arg0, String arg1 )
            {
                Log.v( "onFailure", arg1 );
            }
        } );
    }
}

 

打完收工。

 

最終的效果以下:

device-2015-01-20-171134

 

同志們,頗有成就感了吧!!!

 

不要得意,這只是把數據簡單的顯示出來而已,而且只能是帝都北京的數據,若是你不在北京,這根本對你一點用處都沒有啊。

嗯嗯,今天就到這兒吧,已經足夠多了,好學的你確定已經在查找更多關於Adapter、ListView的知識了,這裏我就不作介紹了,建議你們常常百度。

 

休閒一下,說點廢話。

本人從事IT行業10餘年,從最初級的菜鳥程序員開始,一步一步成長,見過形形色色的程序員,從我的的角度把程序員分紅這麼幾個級別:

1. 無腦

這個指的是一遇到問題就問別人的同志,本身也不思考,也不求上進,混一天算一天。這種類型是極少數的,我也只是據說過,都是傳說中的人物。

2. 菜鳥

這種類型的程序員很是多,一般是接觸技術時間不長的朋友。這部分一般是沒有網絡就不能工做了,常常在論壇裏查找資料,下載別人代碼拿來使用,鍵盤上的Ctrl、C、V鍵都磨光了。

3. 小牛

這種類型多見於技術負責人、技術總監這樣的職位,要想作到小牛這一步,須要對技術有比較深的瞭解,可以從無到有進行設計的。在各類開源社區,這部分人一般會比較活躍,菜鳥們大都使用小牛們的勞動成果。

4. 大牛

這種類型的人已經很是少了,我只見過那麼幾位,都是各大公司CTO之類的。說說其中一位,他從無到有設計了公司的雲計算方案,從硬件的基礎設施到軟件層面,再到用戶層面,這位大牛都有深刻的掌握。

5. 傳說

鄙人只能在各類新聞、書籍中才能見到了。我的認爲這部分人的技術已經不是咱們能夠評判的了,他們的思想、影響力根本不是我等能夠企及的。

 

各位親,你是哪一個級別的?

 

附件是本次的工程文件,點擊下載

 

此係列文章系本人原創,如需轉載,請註明出處 www.liuzhibang.cn

相關文章
相關標籤/搜索