今天终于把天气预报给弄出来了,心里小小的高兴一下,虽然这东西网上早就有了,但那毕竟是别人的。在想做这个之前,以为天气预报软件好高深,不知道从何下手。不过现在想想这东西也不是想象中那么复杂。最主要的是自己亲手做出来,感觉挺很爽的,要是有真机就更好了!
OK,在下面我把天气预报简单叫weather好了,在做weather之前我在网上有搜了一下有关它的资料,知道weather一般的是解析XML文档来获取信息。可能还有别的方式Json.... 然后再结合自己学的Android知识就可以完成本次小程序。本次写的weather里主要涉及到AutoCompleteTextView,ListView以及SAX技术 。先上weather运行时的效果图:
weather布局很丑,哎,俺没啥艺术细胞,只想到这样的布局,重在功能实现上。这次是用SAX解析XML,同样是用到了谷歌提供的天气接口。在上次的学习SAX解析中里面我做的是解析本地的XML,很顺利。在做weather的同时我顺便写了个控制台版的天气预报,发现了有同样的问题:结果只出现温度,而中文信息却是乱码。在这上面我纠结了大半天后来才知道为什么,原因是编码不对,最后换成了GBK编码就搞定了 下面是代码片段。
- URL url = new URL(path);
- URLConnection conn = url.openConnection();
- InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK"); //GBK编码就OK,用utf-8还是出现乱码
- BufferedReader br = new BufferedReader(isr);
- InputSource is = new InputSource(br);
- xmlReader.parse(is);
SimpleAdapter的外部数据(external clients)可以使用这个类将值绑定到视图。你应该用这个类绑定值到那些不能直接通过SimpleAdapter支持的视图,或者改变通过SimpleAdapter支持绑定的方法的视图。
也就是说simpleAdapter不能直接支持ImageView,像TextView就直接支持,不用大费周章绑定数据。
下面是主类继承Activity
- package com.weather.manymore13;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import java.util.Map;
-
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
-
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.XMLReader;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.StaticLayout;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.SimpleAdapter.ViewBinder;
- import android.widget.TextView;
-
- public class WeatherActivity extends Activity {
- private AutoCompleteTextView autoText;
- private Button btnConfirm;
- private TextView tViewCurrent;
- private ListView lv;
- private ArrayList<Map<String,Object>> list;
- private ImageView currentIcon;
- private TextView currentInfo;
- private TextView futionTime;
- private SimpleAdapter simpAdapter = null;
- private boolean visit = true;
- public void init()
- {
- autoText = (AutoCompleteTextView)findViewById(R.id.autoTextView);
- btnConfirm = (Button)findViewById(R.id.btn_confirm);
- tViewCurrent = (TextView)findViewById(R.id.current_time);
- lv = (ListView)findViewById(R.id.myListView);
- currentIcon = (ImageView)findViewById(R.id.current_icon);
- currentInfo = (TextView)findViewById(R.id.current_info);
- futionTime = (TextView)findViewById(R.id.future_time);
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
-
-
- ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
- (this, android.R.layout.simple_dropdown_item_1line,
- Tools.city);
- autoText.setAdapter(arrayAdapter);
- btnConfirm.setOnClickListener(new OnClickListener() {
-
- public void onClick(View arg0) {
-
- System.out.println("按钮被按下");
- String city = autoText.getText().toString().trim();
-
- tViewCurrent.setVisibility(View.INVISIBLE);
- futionTime.setVisibility(View.INVISIBLE);
- if(!(city.equals("")))
- {
- try {
-
-
- obtainWeacherInfo(Tools.SEACH_URL+city);
- if(simpAdapter!=null)
- {
- simpAdapter.notifyDataSetChanged();
- }
-
-
- } catch (MalformedURLException e) {
- System.out.println("url出错");
- e.printStackTrace();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
- });
-
-
- }
-
- public void obtainWeacherInfo(String path) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException
- {
-
- SAXParserFactory parserFactory = SAXParserFactory.newInstance();
-
- SAXParser sp = parserFactory.newSAXParser();
-
- XMLReader xmlReader = sp.getXMLReader();
-
- WeacherHandler handler = new WeacherHandler();
-
- xmlReader.setContentHandler(handler);
-
- URL url = new URL(path);
-
- URLConnection conn = url.openConnection();
-
- InputStreamReader isr = new InputStreamReader(conn.getInputStream(),"GBK");
-
- BufferedReader br = new BufferedReader(isr);
-
- InputSource is = new InputSource(br);
-
- xmlReader.parse(is);
-
- br.close();
-
- displayWeatherInfo(handler);
-
- }
-
-
- public void displayWeatherInfo(WeacherHandler handler)
- {
- list = handler.getForecastWeachers();
-
- //显示实时信息
- Bitmap bmp = handler.getCurrentWeather().getBmp();
- String currentInfoStr = handler.getCurrentWeather().getCurrentInfo().toString();
- currentIcon.setImageBitmap(bmp);
- currentInfo.setText(currentInfoStr);
-
- if(list.size() < 1)
- {
-
- System.out.println("displayWeatherInfo: list.size() < 1");
-
- currentInfo.setText(R.string.prompt);
-
- currentInfo.setTextColor(Color.YELLOW);
-
- currentInfo.setVisibility(View.VISIBLE);
-
-
- }
-
- // 显示未来天气, 我们这里是在ListView中显示
- String[] itemName = new String[]{"week","temperature","condition","icon"};
- int[] itemId = new int[]{R.id.week, R.id.temperature, R.id.condition,R.id.icon};
- simpAdapter = new SimpleAdapter(this,
- list,R.layout.list_item,
- itemName,itemId);
- lv.setAdapter(simpAdapter);
- if(list.size() > 0)
- {
- tViewCurrent.setVisibility(View.VISIBLE);
- futionTime.setVisibility(View.VISIBLE);
- }
- // 注意在这里用到了绑定数据 ImageView绑定了网络图片
- simpAdapter.setViewBinder(new ViewBinder() {
-
- public boolean setViewValue(View view, Object data,
- String textRepresentation) {
- if(view instanceof ImageView&& data instanceof Bitmap){
- ImageView iv = (ImageView) view;
- iv.setImageBitmap((Bitmap) data);
- return true;
- }else {
- return false;
- }
-
- }
-
- });
- }
- }
这东西让我蛋疼了很久,升级后,模拟器当时连个错都不爆,千万不要忘记。