昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码
基本ListView显示
搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面
1 2 3 4 5 6 7 8 9 private void showWeatherInfo () { Bundle bundle = new Bundle(); bundle.putSerializable(Utils.WEATHERINFO, (Serializable) weatherInfoList); Intent intent = new Intent(MyActivity.this , MainActivity.class); intent.putExtra(Utils.WEATHERINFO, bundle); startActivity(intent); finish(); }
如下是用于显示天气信息的MainActivity的内容,注意注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 public class MainActivity extends Activity { private static final String TAG = "MyActivity" ; private ListView mListView; private WeatherAdapter mWeatherAdapter; private EditText mSearchEt; private ArrayList<WeatherInfo> weatherInfoList; private ArrayList<WeatherInfo> resultList; @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); 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); 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)); } } mWeatherAdapter.setWeatherInfoList(resultList); mWeatherAdapter.notifyDataSetChanged(); } } }); } }
看看WeatherAdapter的代码,WeatherAdapter继承BaseAdapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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(); } @Override public WeatherInfo getItem (int i) { return weatherInfoList.get(i); } @Override public long getItemId (int i) { return i; } @Override public View getView (int i, View view, ViewGroup viewGroup) { 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); 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; } public void setWeatherInfoList (List<WeatherInfo> list) { this .weatherInfoList = list; } }
基本效果图:(录屏大师录屏)
源代码下载路径: http://download.csdn.net/detail/poorkick/9510243
缺陷:
每次进入需要载入数据,可以替换成根据时间戳判断是否数据有更新,当然这个对查询借口有点依赖,本地通过数据库存储当前信息
天气信息由于使用的是气象台的,数据更新自己本身无法掌握,可以替换成其他查询接口
后续可能会再做一个更完善的天气预报应用,采用新的查询接口和UI界面 界面简陋,代码不完善,见谅,工作之余练手之作。