昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码

  1. 基本ListView显示
  2. 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面
    private void showWeatherInfo() {
        Bundle bundle = new Bundle();//bundle用来传递weatherinfolist
        bundle.putSerializable(Utils.WEATHERINFO, (Serializable) weatherInfoList);
        Intent intent = new Intent(MyActivity.this, MainActivity.class);
        //跳转到MainActivity
        intent.putExtra(Utils.WEATHERINFO, bundle);
        startActivity(intent);
        finish();//结束当前Activity
    }

如下是用于显示天气信息的MainActivity的内容,注意注释

public class MainActivity extends Activity {

    //定义TAG是用来打Log的
    private static final String TAG = "MyActivity";
    private ListView mListView;//ListView用来显示天气信息
    private WeatherAdapter mWeatherAdapter;//ListView对应的Adapter
    private EditText mSearchEt;//搜索框

    private ArrayList<WeatherInfo> weatherInfoList;//全部天气信息List
    private ArrayList<WeatherInfo> resultList;//根据搜索框输入的内容显示的结果List


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);
        resultList = new ArrayList<WeatherInfo>();

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(Utils.WEATHERINFO);//获取Intent中携带的信息
        weatherInfoList = (ArrayList<WeatherInfo>) bundle.getSerializable(Utils.WEATHERINFO);
        Log.e(TAG, "weatherinfo = " + weatherInfoList.toString());

        mSearchEt = (EditText) findViewById(R.id.et_search);
        mListView = (ListView) findViewById(R.id.lvView);
        mWeatherAdapter = new WeatherAdapter(this, weatherInfoList);
        mListView.setAdapter(mWeatherAdapter);

        //设置TextWathcer()根据输入框的文本变化显示不同的list
        mSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                resultList.clear();
                String searchstr = mSearchEt.getText().toString();
                if(searchstr==null) {
                    mWeatherAdapter.setWeatherInfoList(weatherInfoList);
                    mWeatherAdapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i < weatherInfoList.size(); i++) {
                        String cityname = weatherInfoList.get(i).getCity();
                        if (cityname.contains(searchstr) || searchstr.contains(cityname)) {
                            resultList.add(weatherInfoList.get(i));
                        }
                    }
                    //更新Adatper对应的数据
                    mWeatherAdapter.setWeatherInfoList(resultList);
                    mWeatherAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}

看看WeatherAdapter的代码,WeatherAdapter继承BaseAdapter

//继承BaseAdapter,需要复写几个方法
public class WeatherAdapter extends BaseAdapter {
    private static final String TAG = "WeatherAdapter";
    private Context context;
    private List<WeatherInfo> weatherInfoList;

    public WeatherAdapter(Context context, List<WeatherInfo> weatherInfoList) {
        this.context = context;
        this.weatherInfoList = weatherInfoList;
    }

    @Override
    public int getCount() {
        return weatherInfoList.size();
    }//list的size

    @Override
    public WeatherInfo getItem(int i) {
        return weatherInfoList.get(i);
    }//获取天气信息

    @Override
    public long getItemId(int i) {
        return i;
    }//获取Item的id

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
    //这个方法比较关键,返回Item view
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listviewitem, null);
        }

        ImageView weatherimage = (ImageView) view.findViewById(R.id.weatherimage);
        TextView cityname = (TextView) view.findViewById(R.id.cityname);
        TextView temp = (TextView) view.findViewById(R.id.temp);
        TextView weather = (TextView) view.findViewById(R.id.weather);

        WeatherInfo weatherInfo = weatherInfoList.get(i);
        cityname.setText(weatherInfo.getCity());
        temp.setText(weatherInfo.getTemp1() + " ~ " + weatherInfo.getTemp2());
        String weatherstr = weatherInfo.getWeather();
        weather.setText(weatherstr);
        //这里大致使用几个图片,具体做法,不同的json数据查询api中应该可以查询到支持的天气种类,根据查询到的种类设置不用的图片,这里暂且只分为4种,避免我的apk界面太丑 不忍直视
        if(weatherstr.contains("雨")) {
            weatherimage.setImageResource(R.drawable.rain);
        } else if(weatherstr.contains("阴")) {
            weatherimage.setImageResource(R.drawable.yin);
        } else if(weatherstr.contains("云")) {
            weatherimage.setImageResource(R.drawable.cloud);
        } else if(weatherstr.contains("雪")) {
            weatherimage.setImageResource(R.drawable.snow);
        } else {
            weatherimage.setImageResource(R.drawable.sun);
        }
        return view;

    }
    //方便替换List
    public void setWeatherInfoList(List<WeatherInfo> list) {
        this.weatherInfoList = list;
    }
}

基本效果图:(录屏大师录屏)
这里写图片描述

源代码下载路径:
http://download.csdn.net/detail/poorkick/9510243

缺陷:

  1. 每次进入需要载入数据,可以替换成根据时间戳判断是否数据有更新,当然这个对查询借口有点依赖,本地通过数据库存储当前信息
  2. 天气信息由于使用的是气象台的,数据更新自己本身无法掌握,可以替换成其他查询接口
  3. 后续可能会再做一个更完善的天气预报应用,采用新的查询接口和UI界面 界面简陋,代码不完善,见谅,工作之余练手之作。

results matching ""

    No results matching ""