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

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

界面简陋,代码不完善,见谅,工作之余练手之作。

最新文章

  1. 浅谈 Web 中前后端模板引擎的使用
  2. Android Lint Checks
  3. 面试时被问到js的绑定事件,我居然不知道怎么回答。回来查了下,做个笔记
  4. JS 关闭 页面 浏览器 事件
  5. 取得DIV的ID还是CLASS
  6. 非常全面的讲解Hosts文件
  7. Fragment懒加载
  8. bzoj2791
  9. python学习链接
  10. css动画——transition和animation
  11. SCI科技论文写作技巧-核心价值
  12. centos 安装gcc-&gt;联网 问题解决
  13. Postgresql中的explain
  14. springboot(二十):使用spring-boot-admin对spring-boot服务进行监控
  15. 从壹开始前后端分离 39 || 想创建自己的dotnet模板么?看这里
  16. C/C++音视频库ffmpeg的数据包AVPacket分析
  17. Linux下system()函数的实现
  18. springboot(二十):数据库连接池介绍
  19. Solr7.1---Getting Start
  20. myeclipse maven的联系

热门文章

  1. Java 打包成exe安装包
  2. [poj1811]Prime Test(Pollard-Rho大整数分解)
  3. AngularJs(Part 1)
  4. 25.ProfileService实现(调试)
  5. 利用oracle session context 向oracle传值
  6. Codeforces86D【莫队算法】
  7. 前端需要了解的http知识
  8. 基于Unity 5的次世代卡通渲染技术 -- Unite 2017 米哈游总监贺甲分享实录
  9. 2014 Noip提高组 Day1
  10. MySQL 5.7 Performance Schema 详解