正文
Log.e(\目的\
paramsList.add(new BasicNameValuePair(\
UrlEncodedFormEntity murlEncodedFormEntity=
new UrlEncodedFormEntity(paramsList,HTTP.UTF_8);
httpRequest.setEntity(murlEncodedFormEntity);
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest); Log.e(\添加数据\ //使用GetEntity方法获得返回结果
final int ret=httpResponse.getStatusLine().getStatusCode(); if (ret==HttpStatus.SC_OK) { }
Log.e(\
inputStream=httpResponse.getEntity().getContent(); Log.e(\数据流\数据流\
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (ClientProtocolException e) { e.printStackTrace(); e.printStackTrace(); }}
} catch (IOException e) {
XML解析处理器详细代码参照3.8节。
5.4 天气预报功能模块
在主界面点击天气按钮后进入天气界面,就可以呈现出默认城市(北京)的天气信息。点击城市图标就可以进入城市管理界面,在城市管理界面点击添加按钮就可以进入添加城市界面。选择省份和城市实现添加功能,点击“添加”按钮时就使该城市加入SQLite数据库中,并且显示提示是否成功。点击添加的城市跳转到主界面,就可以查询到该城市的天气信息。同时。长按列表条目可以实现删除功能。主界面的刷新按钮,点击可以获得最新的天气情况。
32
正文
天气预报主界面如下:
图5-14 主界面
33
正文
各个界面之间的联系图如下:
图5-15 天气预报功能图
34
正文
核心代码:
天气预报信息查询功能代码参照3.6节 1. 实现城市添加功能,代码如下: //确定按钮点击事件 //省份
private void initprovince() {
Province=(Spinner)findViewById(R.id.province); //将ARRAY.XML读取为数组
//设置适配器
ProvinceAdapter=ArrayAdapter.createFromResource(this,
//设置样式
CitySure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { }});
city=new LifeSqlite(WeatherCitySetting.this); city.openMyDatabse(); city.insertCITY(CityName); city.close();
Intent intent =new Intent(WeatherCitySetting.this, WeatherCityActivity.class); startActivity(intent);
ArrayAdapter
R.array.province,android.R.layout.simple_spinner_item);
ProvinceAdapter.setDropDownViewResource Province.setAdapter(ProvinceAdapter);
Province.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ });
Province.setVisibility(View.VISIBLE);
35
(android.R.layout.simple_spinner_dropdown_item);
@Override
public void onItemSelected(AdapterView> parent, View view, } @Override
public void onNothingSelected(AdapterView> parent) { }
int position, long id) { initcity(position);
parent.setVisibility(View.VISIBLE);
正文
}
//城市
private void initcity(int code) {
// TODO Auto-generated method stub City=(Spinner)findViewById(R.id.city); int[] cityarray={R.array.zhejiang, R.array.hubei,
R.array.hunan, R.array.beijing, R.array.shanghai, R.array.tianjing, R.array.chongqing, R.array.heilongjiang, R.array.jilin, R.array.liaoning, R.array.neimenggu, R.array.hebei, R.array.shanxi, R.array.shangxi, R.array.shandong, R.array.xinjiang, R.array.xizang, R.array.qinghai, R.array.gansu, R.array.ningxia, R.array.henan, R.array.jiangsu, R.array.anhui, R.array.fujian, R.array.jiangxi, R.array.guizhou, R.array.sichuan, R.array.guangdong, R.array.yunnan, R.array.guangxi, R.array.hainan, R.array.xianggang, R.array.aomen, R.array.taiwan,
};
//设置适配器
ArrayAdapter CityAdapter =
ArrayAdapter.createFromResource(this, cityarray[code], }
36
}
android.R.layout.simple_spinner_item);
//设置样式
CityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); City.setAdapter(CityAdapter);
City.setOnItemSelectedListener(new OnItemSelectedListener() { });
City.setVisibility(View.VISIBLE);
@Override
public void onItemSelected(AdapterView> parent, View view, } @Override
public void onNothingSelected(AdapterView> arg0) { }
int position, long id) {
// TODO Auto-generated method stub
CityName = (String) City.getItemAtPosition(position); parent.setVisibility(View.VISIBLE);