这可能是我见过最简单的天气预报项目

先上张图让大家看一看布局的方式

这可能是我见过最简单的天气预报项目_第1张图片
这可能是我见过最简单的天气预报项目_第2张图片
示意图上写的非常清楚了,横向listview里边再加一个横向的scrollview,需要注意的就是触摸事件的分发,我是通过ev.getY来判断高度来区别响应ListView本身的滑动还是ScrollView的滑动。

在这里我只是分点的把一些讲一下,看完再去看程序源码绝对轻松简单。

首先,你得有全国的省市的ID和名字,通过城市ID来从API获取天气数据
http://blog.csdn.net/d_dmelon/article/details/51361416
这本身就是个Activity,可以用startActivityforResult来启动,并且返回城市的Name和ID,通过id访问接口,获取该城市的天气情况。
然后就是怎么获取天气,我用的是这个接口,地址最后边的数字是城市id,北京的ID是101010100,如下
http://wthrcdn.etouch.cn/weather_mini?citykey=101010100

数据如下:

{
    "desc":"OK",
    "status":1000,
    "data":
        { "wendu":"26", "ganmao":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。", "forecast": [ {"fengxiang":"无持续风向", "fengli":"微风级", "high":"高温 29℃", "type":"晴", "low":"低温 15℃", "date":"19日星期四" }, {"fengxiang":"无持续风向", "fengli":"微风级", "high":"高温 30℃", "type":"晴", "low":"低温 15℃", "date":"20日星期五" }, {"fengxiang":"无持续风向","fengli":"微风级","high":"高温 30℃", "type":"晴","low":"低温 15℃","date":"21日星期六" }, {"fengxiang":"无持续风向","fengli":"微风级","high":"高温 28℃", "type":"多云","low":"低温 17℃","date":"22日星期天" }, {"fengxiang":"无持续风向","fengli":"微风级","high":"高温 25℃", "type":"阴","low":"低温 15℃","date":"23日星期一" } ], "yesterday": { "fl":"微风","fx":"无持续风向","high":"高温 29℃","type":"晴","low":"低温 18℃","date":"18日星期三" }, "aqi":"137", "city":"北京" } }

有了数据,就得显示吧,解析上边的JSON数据看看我们能获取什么有用的信信息。当天温度(wendu),和接下来5天的感冒、风向,风力、高低温、天气情况(type)。天气情况用图片显示,其他的用文字描述。这样我们要在listview里存的data已经很明确了。
资源文件的resID Int型 和 天气情况的文字描述 String型。
所以data应该是个HashMap

            HashMap<String,Object> item = new HashMap<String,Object>();
            item.put("ftype",Rfirpng);// String ,Int
            item.put("stype",Rsecpng);
            item.put("ttype",Rthrpng);
            item.put("fotype",Rfoupng);
            item.put("fitype",Rfifpng);
            item.put("name",strName);
            item.put("ID",strID);
            item.put("wendu",wendu);//String ,String
            item.put("fengxiang",fengxiang);
            item.put("ganmao",ganmao);
            item.put("finfo",finfo);
            item.put("sinfo",sinfo);
            item.put("tinfo",tinfo);
            item.put("foinfo",foinfo);
            item.put("fiinfo",fiinfo);
            mainList.add(item);
            saveListData();
        String[] Keys = new String[]{"ftype","ftype","stype","ttype","fotype","fitype"
                ,"name","wendu","fengxiang","ganmao","finfo","sinfo","tinfo","foinfo","fiinfo"};
        int[] Ids = new int[]{R.id.pngtype,R.id.firpng,R.id.secpng,R.id.thrpng,R.id.foupng,R.id.fifpng
                ,R.id.text_name,R.id.text_wendu,R.id.text_fengxiang,R.id.text_ganmao
                ,R.id.firdayinfo,R.id.secdayinfo,R.id.thrdayinfo,R.id.foudayinfo,R.id.fifdayinfo};

        mainAdapter = new SimpleAdapter(this,mainList,R.layout.weather,Keys,Ids);
//        mAdapter = new HorizontalSimpleAdapter(this,mainList,R.layout.weather,Keys,Ids);
        mainListView.setAdapter(mainAdapter);
//        mainListView.setAdapter(mAdapter);

恩恩,到这里就差不多能显示了,但天气预报不仅仅如此,看到我上边贴出来的代码里 saveListData这个函数,是不是很奇怪啊,这就是接下来我要说的,通过SharedPreferences来保存天气信息和是否第一次进入(如果要扩展,以后做过第一次进入的引导界面也是OK的)。
那么到底要保存哪些信息?
1是Json解析出来我们要显示的图片的resID和文字描述。
2是是否第一次进入,布尔值,如果第一次进入默认网络请求加载北京市的城市天气
3是上次我们申请刷新天气的时间,如果没有这个判断,你过几天再登进去看就是过时的信息啦哈哈

    public void saveListData(){ //保存数据
        SharedPreferences.Editor editor = getSharedPreferences("ArrayData",MODE_PRIVATE).edit();
        SharedPreferences getData = getSharedPreferences("ArrayData",MODE_PRIVATE);
//        int oldSize =getData.getInt("NUMBER",0);
        editor.putInt("NUMBER",mainList.size());//已有的Item数目
        for(int i=0;i<mainList.size();i++){ //把图片的R.ID存进去

            editor.putInt("Item"+i+"ftype",(int)mainList.get(i).get("ftype"));
            editor.putInt("Item"+i+"stype",(int)mainList.get(i).get("stype"));
            editor.putInt("Item"+i+"ttype",(int)mainList.get(i).get("ttype"));
            editor.putInt("Item"+i+"fotype",(int)mainList.get(i).get("fotype"));
            editor.putInt("Item"+i+"fitype",(int)mainList.get(i).get("fitype"));
            editor.putString("Item"+i+"name",(String)mainList.get(i).get("name"));
            editor.putString("Item"+i+"ID",(String)mainList.get(i).get("ID"));
            editor.putString("Item"+i+"wendu",(String)mainList.get(i).get("wendu"));
            editor.putString("Item"+i+"fengxiang",(String)mainList.get(i).get("fengxiang"));
            editor.putString("Item"+i+"ganmao",(String)mainList.get(i).get("ganmao"));
            editor.putString("Item"+i+"finfo",(String)mainList.get(i).get("finfo"));
            editor.putString("Item"+i+"sinfo",(String)mainList.get(i).get("sinfo"));
            editor.putString("Item"+i+"tinfo",(String)mainList.get(i).get("tinfo"));
            editor.putString("Item"+i+"foinfo",(String)mainList.get(i).get("foinfo"));
            editor.putString("Item"+i+"fiinfo",(String)mainList.get(i).get("fiinfo"));
        }
        editor.commit();

        Calendar c = Calendar.getInstance();
        int saveHour = c.get(Calendar.HOUR_OF_DAY);
        SharedPreferences.Editor settingEditor = getSharedPreferences("data",MODE_PRIVATE).edit();
        settingEditor.putInt("saveHour",saveHour);
        settingEditor.commit();
    }
    public boolean Isfirst(){
        SharedPreferences settings = getSharedPreferences("data",MODE_PRIVATE);
        Boolean First = settings.getBoolean("IsFirstIn",true);//没有字段则为第一次进入
        return First;
    }

有save肯定有get啦

public void getListData(){ //获取数据
        SharedPreferences getData = getSharedPreferences("ArrayData",MODE_PRIVATE);
        int Size =getData.getInt("NUMBER", 0);//若取不到NUMBER值 则默认为0

        if(checkUpdate()) {
            for (int i = 0; i < Size; i++) {
                strID = getData.getString("Item"+i+"ID","NULL");
                strName = getData.getString("Item"+i+"name","NULL");
                WeatherUrl = WEATHER_URL + strID;
                sendRequestWithHttpURLConnection();
                try{
                    Thread.sleep(200);
                }catch (Exception e ){e.printStackTrace();}

            }
        }
        else{
            for(int i=0;i<Size;i++){
                HashMap<String,Object> item = new HashMap<String,Object>();

                item.put("today",getData.getInt("Item"+i+"today",R.drawable.daxue));
                item.put("ftype",getData.getInt("Item"+i+"ftype",R.drawable.daxue));
                item.put("stype",getData.getInt("Item"+i+"stype",R.drawable.daxue));
                item.put("ttype",getData.getInt("Item"+i+"ttype",R.drawable.daxue));
                item.put("fotype",getData.getInt("Item"+i+"fotype",R.drawable.daxue));
                item.put("fitype",getData.getInt("Item"+i+"fitype",R.drawable.daxue));
                item.put("name",getData.getString("Item"+i+"name","NULL"));
                item.put("ID",getData.getString("Item"+i+"ID","NULL"));
                item.put("wendu",getData.getString("Item"+i+"wendu","NULL"));
                item.put("fengxiang",getData.getString("Item"+i+"fengxiang","NULL"));
                item.put("ganmao",getData.getString("Item"+i+"ganmao","NULL"));
                item.put("finfo",getData.getString("Item"+i+"finfo","NULL"));
                item.put("sinfo",getData.getString("Item"+i+"sinfo","NULL"));
                item.put("tinfo",getData.getString("Item"+i+"tinfo","NULL"));
                item.put("foinfo",getData.getString("Item"+i+"foinfo","NULL"));
                item.put("fiinfo",getData.getString("Item"+i+"fiinfo","NULL"));
                mainList.add(item);
            }
        }
    }

知识有限,存取的方法可能比较low,不过功能倒是实现了~~

源码源码

APK地址

你可能感兴趣的:(android,应用)