Android项目 之 天气预报(附源码)

编程环境:Eclipse3.5.2 + Android 2.3.3 + Linux 3.0.0 (Ubuntu 11.10)

架构图:

主界面显示一个当前时间的天气信息和后四天的天气信息。天气信息来源Google的天气API。

publicstaticfinalStringqueryString= "http://www.google.com/ig/api?hl=zh-cn&weather=";

考虑到网络的延迟和用户的体验,从服务器上读取信息的代码在线程中进行和用Handler处理消息:

publicvoidrun() {

mHandler.sendEmptyMessage(RUNNABLESTART);

String cityParamString = ConstData.cityCode[this.city_index];

try{

URL url =newURL(ConstData.queryString+ cityParamString);

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

GoogleWeatherHandler gwh =newGoogleWeatherHandler();

xr.setContentHandler(gwh);

InputStreamReader isr =newInputStreamReader(url.openStream(),

"GBK");

InputSource is =newInputSource(isr);

xr.parse(is);

my_ws = gwh.getMyWeatherSet();

mHandler.sendEmptyMessage(SHOWINFORMATION);

}catch(UnknownHostException e){

mHandler.sendEmptyMessage(LOSTHOST);

}catch(Exception e) {

e.printStackTrace();

}

}

Handler mHandler =newHandler() {

@Override

publicvoidhandleMessage(Message msg) {

super.handleMessage(msg);

switch(msg.what) {

caseLOSTHOST:

pd.dismiss();

Toast.makeText(MainActivity.this, "无法连接服务器,请检查您的网络", Toast.LENGTH_LONG).show();

break;

caseSHOWINFORMATION:

try{

if(my_ws.getMyCurrentCondition()!=null){

updateCurrWeather(my_ws.getMyCurrentCondition());

updateInformation(my_ws.getInformation());

updateWeatherInfoView(R.id.weather_1, my_ws.getMyForecastConditions().get(0));

updateWeatherInfoView(R.id.weather_2, my_ws.getMyForecastConditions().get(1));

updateWeatherInfoView(R.id.weather_3, my_ws.getMyForecastConditions().get(2));

updateWeatherInfoView(R.id.weather_4, my_ws.getMyForecastConditions()get(3));

}else{

Toast.makeText(MainActivity.this, "没有该城市信息!", Toast.LENGTH_LONG).show();

}

}catch(Exception e) {

e.printStackTrace();

}

pd.dismiss();

mThread.stop();//非线程安全,但在这次APP中,不会有什么问题。

break;

caseRUNNABLESTART:

pd = ProgressDialog.show(MainActivity.this, "请稍等", "正在加载信息...",true);

break;

}

}

主界面有两个按钮,对应两个功能,非别是 选择城市 和 我的城市

1)选择城市

弹出一个框,可以自己搜索想要的城市。为方便输入,输入框使用了AutoCompleteTextView。(虚拟机不支持中文,所以这里使用了城市名的全拼)

@Override

protectedDialog onCreateDialog(intid) {

LayoutInflater dialoglayout = LayoutInflater.from(this);

finalView textEntryView = dialoglayout.inflate(R.layout.dialog,null);

ArrayAdapter<String> adapter =newArrayAdapter<String>(this,

android.R.layout.simple_dropdown_item_1line, ConstData.cityCode);

/*findByView方法时 要注意布局关系*/

finalAutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) textEntryView.findViewById(R.id.search);

autoCompleteTextView.setAdapter(adapter);

returnnewAlertDialog.Builder(this)

.setView(textEntryView)

.setIcon(R.drawable.search)

.setTitle("请输入城市")

.setPositiveButton(R.string.set_to_mycity,newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface dialog,intwhichButton) {

city_index = convertCity_nameToCity_code(autoCompleteTextView.getText().toString());

SharedPreferences.Editor editor = sharedPreferences.edit();

String key = "MyCitys"+city_index;

if(!sharedPreferences.contains(key)){

try{

editor.putString(key, ConstData.city[city_index]);

editor.commit();

}catch(ArrayIndexOutOfBoundsException e){

Toast.makeText(MainActivity.this, "城市名有誤!",Toast.LENGTH_LONG).show();

}

}

mThread.run();

}

})

.setNegativeButton(R.string.start_search,newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface dialog,intwhichButton) {

city_index = convertCity_nameToCity_code(autoCompleteTextView.getText().toString());

Log.i("tag","autoCompleteTextView.getListSelection() = "+city_index);

if( city_index >= 0 ) mThread.run() ;

elseToast.makeText(MainActivity.this, "获取城市信息失败", Toast.LENGTH_LONG);

}

})

.create();

}

 



2)我的城市

可以将自己所在的城市或者关注的城市添加到此,方便日后的查询。这里用了ListView控件显示数据,数据存储在SharedPreferences中。

list =newArrayList<String>();

lv =newListView(this);

sharedPreferences = getSharedPreferences(PREFERENCE_NAME,MODE);

editor = sharedPreferences.edit();

init();

lv.setOnItemClickListener(newOnItemClickListener(){ //单击监听事件

publicvoidonItemClick(AdapterView<?> arg0, View arg1,intpos,

longarg3) {

Intent intent =newIntent(ListOfMyCity.this, MainActivity.class);

String name = list.get(pos);

inti ;

for(i=0; i < ConstData.city.length; i++){

if(name.equals(ConstData.city[i])){

editor.putInt("DefaultCity", i);

editor.commit();

break;

}

}

intent.putExtra("city_code", i);

setResult(8, intent);

finish();

}});

lv.setOnItemLongClickListener(newOnItemLongClickListener(){//长按事件 触发“删除”和“设为默认城市” 事件

publicbooleanonItemLongClick(AdapterView<?> arg0, View arg1,

intpos,longarg3) {

showDialog(pos);

returnfalse;

}});

 

 

 

删除可以将城市从 数据中删除,设为默认城市后,每次启动APP都会自动检索到该城市。

主界面:

 

 

代码:http://download.csdn.net/detail/cliffbaby/4003285

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值