1 实验目的。
输入要查询的城市名称,点击查询按钮后,依次出现七天的天气情况。
2 实验过程。
1、首先搞定天气预报数据来源的问题,提高天气预报服务的有很多网站,这些网站一般都会提供比较详细的 API 接口供应用程序调用。
3 代码部分。
activity_weather.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/city"
tools:context=".WeatherActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/etCity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/etcity"
android:drawablePadding="5dp"
android:ems="10"
android:hint="@string/etCity" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/btnQuery"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="@null"
android:src="@drawable/serch" />
</LinearLayout>
<ListView
android:id="@+id/lvFutureWeather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:dividerHeight="10dp"
android:layoutAnimation="@anim/weather_list_layout_animation" >
</ListView>
<span style="color:#ff0000;"></RelativeLayout></span>
activity_weather_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/list_item_shape"
android:padding="10dp" >
<TextView
android:id="@+id/tvDayofWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:text="星期日" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvDayofWeek"
android:layout_alignBottom="@+id/tvDayofWeek"
android:layout_alignParentRight="true"
android:text="20160207" />
<TextView
android:id="@+id/tvTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvDayofWeek"
android:layout_below="@+id/tvDayofWeek"
android:layout_marginTop="15dp"
android:text="temperature" />
<TextView
android:id="@+id/tvWeather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvTemperature"
android:layout_below="@+id/tvTemperature"
android:layout_marginTop="15dp"
android:text="weather" />
</RelativeLayout>
Weather.java
package com.example.weather.moder;
public class Weather {
private String dayOfWeek;//星期几
private String date;//日期
private String temperature;//温度
private String weather;//天气
public Weather(){
}
public Weather(String dayOfWeek, String date, String temperature,
String weather) {
super();
this.dayOfWeek = dayOfWeek;
this.date = date;
this.temperature = temperature;
this.weather = weather;
}
public String getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
@Override
public String toString() {
return "Weather [dayOfWeek=" + dayOfWeek + ", date=" + date
+ ", temperature=" + temperature + ", weather=" + weather + "]";
}
}
4 实验注意事项:在进行查询输入城市名时一定要先改编码格式 列如
<span style="font-size:18px;">cityName = URLEncoder.encode("滨州", "utf-8");</span>