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

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

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

<Android 应用 之路> 天气预报(三)的更多相关文章

  1. <Android 应用 之路> 天气预报(五)

    前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...

  2. <Android 应用 之路> 天气预报(四)

    前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...

  3. <Android 应用 之路> 天气预报(一)

    Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...

  4. <Android 应用 之路> 天气预报(二)

    界面组成 载入界面 显示界面 Activity两个,一个用来显示载入界面,一个用来显示天气信息 主要代码如下: public class MyActivity extends Activity { p ...

  5. Android学习之路——简易版微信为例(一)

    这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...

  6. ❤️【Android精进之路-01】定计划,重行动来学Android吧❤️

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. Android精进之路第一篇,确定安卓学习计划. 干货满满,建议收藏,需要用到时常看看.小伙伴们如有问题及需要,欢迎踊跃留言哦~ ~ ~. 前言 ...

  7. redis成长之路——(三)

    redis连接封装 StackExchange.Redis中有一些常功能是不在database对中,例如发布订阅.获取全部key(本代码中已封装到operation中了)等,而且StackExchan ...

  8. Android实现全屏的三种方式

    一.通过代码 requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏 getWindow().setFlags(WindowManager.Lay ...

  9. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

随机推荐

  1. 好文章!转载嵌入式LINUX

    整理了嵌入式linux学习路线供参考,希望对您有所参考价值! 一.linux入门 目前嵌入式主要开发环境有 Linux.Wince等:Linux因其开源.开发操作便利而被广泛采用.而Linux操作系统 ...

  2. Java类加载原理解析(转)

    1 基本信息 每个开发人员对java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载.Java的类加载机制是技术体系中比较核心的 ...

  3. (十四)hibernate逆向工程

    一.hibernate逆向工程生成实体 介绍一个模型设计工具PowerDesigner,这个是j2ee开发必要的一个工具.一般在开发中先使用PowerDesigner 创建实体关系图即概念模型.建立了 ...

  4. hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)

    TIANKENG’s restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Ja ...

  5. Maximum Cardinality Bipartite Matching: Augmenting Path Algorithm

    http://www.csie.ntnu.edu.tw/~u91029/Matching.html int nx,ny; int mx[N],my[N]; bool vy[N]; bool g[N][ ...

  6. 2-1 本章作业&2-2 开发系统与工具选择

    2-1 2-2 推荐使用Android Studio开发Flutter

  7. 解析Xml文件的三种方式

    1.Sax解析(simple api  for xml) 使用流式处理的方式,它并不记录所读内容的相关信息.它是一种以事件为驱动的XML API,解析速度快,占用内存少.使用回调函数来实现. clas ...

  8. django 模版 语法与使用

    目录 django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  9. java基础第四篇之面向对象

    7.封装与面向对象 a.方法: public static void main(String[] args) { } 一般定义标准: 形参:一般把 不确定的量或者变化的量定义在形参位置//圆的的半径, ...

  10. ffmpeg命令操作音频格式转换

    1.转MP3为wav ffmpeg -i input.mp3 -acodec pcm_s16le -ac 2 -ar 44100 output.wav 2.转m4a为wav ffmpeg -i inp ...