<Android 应用 之路> 天气预报(三)
昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码
- 基本ListView显示
- 搜索框,查询城市
上一篇文章中,载入界面通过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界面
界面简陋,代码不完善,见谅,工作之余练手之作。
<Android 应用 之路> 天气预报(三)的更多相关文章
- <Android 应用 之路> 天气预报(五)
前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...
- <Android 应用 之路> 天气预报(四)
前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...
- <Android 应用 之路> 天气预报(一)
Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...
- <Android 应用 之路> 天气预报(二)
界面组成 载入界面 显示界面 Activity两个,一个用来显示载入界面,一个用来显示天气信息 主要代码如下: public class MyActivity extends Activity { p ...
- Android学习之路——简易版微信为例(一)
这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...
- ❤️【Android精进之路-01】定计划,重行动来学Android吧❤️
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. Android精进之路第一篇,确定安卓学习计划. 干货满满,建议收藏,需要用到时常看看.小伙伴们如有问题及需要,欢迎踊跃留言哦~ ~ ~. 前言 ...
- redis成长之路——(三)
redis连接封装 StackExchange.Redis中有一些常功能是不在database对中,例如发布订阅.获取全部key(本代码中已封装到operation中了)等,而且StackExchan ...
- Android实现全屏的三种方式
一.通过代码 requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏 getWindow().setFlags(WindowManager.Lay ...
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
随机推荐
- u-boot.lds 链接脚本分析(hi3515)
目录:/u-boot_hi3515/board/hi3515v100 OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm& ...
- mysql两主多从
1.实现目标 目标清单: 1)Master(192.168.31.230)为正常运行环境下的主库,为两个Slave(192.168.31.231和192.168.31.232)提供“主-从”复制功能: ...
- Linux下使用doxygen+vim生成c语言源程序文档的方法
1.安装 doxygen 有两种获得 doxygen 的方法.可以下载预编译的可执行文件,也可以从 SVN 存储库下载源代码并自己编译.清单 1 演示的是后一种方法. 清单 1. 安装和构建 doxy ...
- 快速部署Kubernetes集群管理
这篇文章介绍了如何快速部署一套Kubernetes集群,下面就快速开始吧! 准备工作 //关闭防火墙 systemctl stop firewalld.service systemctl disabl ...
- Elastic-job使用及原理
一.原理 elastic-job有lite版和cloud版,最大的区别是有无调度中心,笔者采用的是lite版本,无中心化. tips: 第一台服务器上线触发主服务器选举.主服务器一旦下线,则重新触发选 ...
- Unity ShadowMapping
原理: 场景中一个plane,一个cube,一个light,一个mainCamera 为了在plane上呈现cube的shadow,先在light上放一个lightCamera(位置方向跟light相 ...
- 洛谷P2652 同花顺
P2652 同花顺 题目背景 所谓同花顺,就是指一些扑克牌,它们花色相同,并且数字连续. 题目描述 现在我手里有n张扑克牌,但它们可能并不能凑成同花顺.我现在想知道,最少更换其中的多少张牌,我能让这 ...
- poj1837 Balance
Balance POJ - 1837 题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 其中可以把天枰看做一个以x轴0点 ...
- pytest框架(一)
代码示例一 # coding=utf-8 def func(x): return x + 1 def test_answer(): assert func(3) == 5 运行结果 E:\pyYouY ...
- JAVA之动态编译
通过Java动态生成class文件 今天说下JAVA中的动态编译,这个功能根据我现在的了解好像没有见到过用的,我Jio的吧,现在的一些在线代码编缉器可以用到了,这个具体我也不是很清楚.感兴趣的大家可以 ...