运行效果
activity_weather_listitem.xml
c?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>
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>
</RelativeLayout>
weather.java
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 + "]";
}
}
WeatherAdapter.java
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.weather.R;
import com.example.weather.moder.Weather;
public class WeatherAdapter extends ArrayAdapter<Weather> {
private int resourceId;
public WeatherAdapter(Context context, int textViewResourceId,
List<Weather> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup viewgroup) {
Weather weather = getItem(position);
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(resourceId,
null);
viewHolder.tvDayOfWeek = (TextView) convertView
.findViewById(R.id.tvDayofWeek);
viewHolder.tvDate = (TextView) convertView
.findViewById(R.id.tvDate);
viewHolder.tvTemperature = (TextView) convertView
.findViewById(R.id.tvTemperature);
viewHolder.tvWeather = (TextView) convertView
.findViewById(R.id.tvWeather);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvDayOfWeek.setText(weather.getDayOfWeek());
viewHolder.tvDate.setText(weather.getDate());
viewHolder.tvTemperature.setText(weather.getTemperature());
viewHolder.tvWeather.setText(weather.getWeather());
return convertView;
}
private class ViewHolder {
TextView tvDayOfWeek;
TextView tvDate;
TextView tvTemperature;
TextView tvWeather;
}
}
WeatherActivity.java
package com.example.weather.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.weather.R;
import com.example.weather.moder.Weather;
public class WeatherAdapter extends ArrayAdapter<Weather> {
private int resourceId;
public WeatherAdapter(Context context, int textViewResourceId,
List<Weather> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup viewgroup) {
Weather weather = getItem(position);
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(resourceId,
null);
viewHolder.tvDayOfWeek = (TextView) convertView
.findViewById(R.id.tvDayofWeek);
viewHolder.tvDate = (TextView) convertView
.findViewById(R.id.tvDate);
viewHolder.tvTemperature = (TextView) convertView
.findViewById(R.id.tvTemperature);
viewHolder.tvWeather = (TextView) convertView
.findViewById(R.id.tvWeather);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvDayOfWeek.setText(weather.getDayOfWeek());
viewHolder.tvDate.setText(weather.getDate());
viewHolder.tvTemperature.setText(weather.getTemperature());
viewHolder.tvWeather.setText(weather.getWeather());
return convertView;
}
private class ViewHolder {
TextView tvDayOfWeek;
TextView tvDate;
TextView tvTemperature;
TextView tvWeather;
}
}
HttpCallbackListener.java
public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
HttpUiti.java
public class HttpUtil {
public static void sendHttpRequest(final String address,
final HttpCallbackListener listener) {
new Thread(new Runnable() {
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
if (listener != null) {
// 回调 onFinish()方法
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener != null) {
// 回调 onError()方法
listener.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
}
同样应加入上网权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />