WeatherActivity:
package com.weatherdemo.New; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import com.weatherdemo.R; import com.weatherdemo.WeatherDemoLogic; import com.weatherdemo.zidingyi.CityName; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class WeatherActivity extends Activity { Button bt, future_bt, dangqian_bt; EditText cityname, dangqian_et, today_et; int i = 1; WeatherDemoLogic wLogic = null; List<WeatherDemo> list = new ArrayList<WeatherDemo>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.weather); dangqian_bt = (Button) findViewById(R.id.dangqian_bt); bt = (Button) findViewById(R.id.today_bt); future_bt = (Button) findViewById(R.id.Future_bt); cityname = (EditText) findViewById(R.id.cityname_et); dangqian_et = (EditText) findViewById(R.id.dangqian_weather); today_et = (EditText) findViewById(R.id.today_weather); wLogic = new WeatherDemoLogic(); /** * 当前天气实况 */ dangqian_bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { list = wLogic.WeatherDemo(cityname.getText().toString()); dangqian_et .setText("当前温度: " + list.get(0).getwNew().getTemp() + "\n" + "当前风向: " + list.get(0).getwNew().getWind_direction() + "\n" + "当前风力: " + list.get(0).getwNew().getWind_strength() + "\n" + "当前湿度: " + list.get(0).getwNew().getHumidity() + "\n" + "更新时间: " + list.get(0).getwNew().getTime()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }); /** * 今天天气 */ bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { list = wLogic.WeatherDemo(cityname.getText().toString()); WeatherNew arr = list.get(0).getwNew(); today_et.setText("城市: " + arr.getCity() + "\n" + "日期: " + arr.getDate_y() + " " + arr.getWeek() + "\n" + "今日温度: " + arr.getTemperature() + "\n" + "今日天气: " + arr.getWeather() + "\n" + "风: " + arr.getWind() + "\n" + "穿衣指数: " + arr.getDressing_index() + "\n" + "穿衣建议: " + arr.getDressing_advice() + "\n" + "紫外线强度: " + arr.getUv_index() + "\n" + "舒适度指数: " + arr.getComfort_index() + "\n" + "洗车指数: " + arr.getWash_index() + "\n" + "旅游指数: " + arr.getTravel_index() + "\n" + "晨练指数: " + arr.getExercise_index() + "\n" + "干燥指数: " + arr.getDrying_index()); } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); /** * 未来天气 */ future_bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { list = wLogic.WeatherDemo(cityname.getText().toString()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } List<Map<String, String>> arr = list.get(0).getList(); if (i < 7) { today_et.setText("温度: " + arr.get(i).get("temperature_future") + "\n" + "天气状况: " + arr.get(i).get("weather_future") + "\n" + "风: " + arr.get(i).get("wind_future") + "\n" + "日期: " + arr.get(i).get("date_future")+" "+arr.get(i).get("week_future")); i = i + 1; } else { Toast.makeText(WeatherActivity.this, "抱歉!超出预测天数!!!", Toast.LENGTH_SHORT).show(); } } }); } class EditextAdapter extends BaseAdapter{ Context context; public EditextAdapter() { } public EditextAdapter(Context context) { this.context=context; } @Override public int getCount() { return CityName.name.length; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { HolderView holder=null; if(convertView==null){ convertView=LayoutInflater.from(WeatherActivity.this).inflate(R.layout.demo, null); holder=new HolderView(convertView); convertView.setTag(holder); }else{ holder=(HolderView) convertView.getTag(); } holder.tv.setText(CityName.name[position].toString()); return convertView; } } class HolderView{ TextView tv; public HolderView(View v){ tv=(TextView) v.findViewById(R.id.ctiyname_tv); } } }
weather.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/cityname_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入城市名" > <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/dangqian_bt" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="dang qian" /> <Button android:id="@+id/today_bt" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="Today" /> <Button android:id="@+id/Future_bt" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="Next_day" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前天气:"/> <EditText android:id="@+id/dangqian_weather" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:ems="10" android:hint="当前天气实况" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今天天气或未来七天天气:"/> <EditText android:id="@+id/today_weather" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:ems="10" android:hint="今天天气及未来六天天气" /> </LinearLayout>