10天学安卓-第四天

继续昨天的学习。

昨天我们根据取得的天气数据新建了一个视图用来显示各项内容,那么今天我们就把数据显示出来吧!!!

这里我们要把数据和视图联系起来,那么就用到了适配器-Adapter,Android给我们提供了很多Adapter,这里我们用到了BaseAdapter。

 

BaseAdapter(1)

右键点击src/com.demo.weather,选择 New > Class,按照下图填写:

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有几条数据,

 

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 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

posted on 2015-01-20 17:14  Game_over  阅读(2437)  评论(8编辑  收藏  举报