1. You need to obtain InterNet permissions in the Androidmanifest.xml manifest file
\
Create refreshListView.java from ListView
package com.t20.weather.view;
import com.t20.weather.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
public class RefreshListView extends ListView {
private int mHeight;
private View footview;
public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
// Load the initialization layout
intiRefreshListView();
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// Load the initialization layout
intiRefreshListView();
}
public RefreshListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
// Load the initialization layout
intiRefreshListView();
}
/** * Initialize layout */
public void intiRefreshListView(a){
// Load layout (used to display loaded ICONS)
footview=View.inflate(getContext(),R.layout.refresh_foot_view, null);
// Append the layout to the bottom of the ListView
addFooterView(footview);
// Use a ruler to measure height (lower versions of Android have a serious Bug that does not support relative layout, fixed after Level 17)
footview.measure(0.0);
//------- wide offset -- high offset
// Get the measured height
mHeight=footview.getMeasuredHeight();
// Hide the refresh_foot_view.xml layout by setting the inside margin
footview.setPadding(0, -mHeight, 0.0);
// Set the ListView to listen for scroll events
this.setOnScrollListener(new OnScrollListener() {
/** * The scroll state changes */
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
/** ** */
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
// When the ListView scrolls to the bottom
if(getLastVisiblePosition()==getCount()-1) {// Make the refresh_foot_view.xml layout display (display loading)
footview.setPadding(0.0.0.0);
// Determine whether network data is being requested
if(loadingFlag==false) {if(onRefreshListener! =null){
onRefreshListener.RefeshData();
loadingFlag=true;// Requesting}}}}}); }// Whether the network is being requested to prevent repeated requests (true: the network is being requested, false: the network is not requested)
private boolean loadingFlag=false;
/** * The method to execute when data is loaded */
public void loadFinish(a){
// Hide the refresh_foot_view.xml layout by setting the inside margin
footview.setPadding(0, -mHeight, 0.0);
// Request complete
loadingFlag=false;
}
private OnRefreshListener onRefreshListener;
/ * * *@param onRefreshListener the onRefreshListener to set
*/
public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
this.onRefreshListener = onRefreshListener;
}
/** * Define an interface to refresh network data *@author Administrator
*
*/
public interface OnRefreshListener{
void RefeshData(a); }}Copy the code
Create Weather entity class weather.java (used to encapsulate the Weather information obtained by the server)
package com.t20.weather.entity;
import java.io.Serializable;
/** * Weather entities *@author admin
*
*/
public class Weather implements Serializable {
private Integer id; // City code
private String city; / / city name
private String tianqi; / / the weather
private double temp; / / temperature
private String img; // Weather icon
public Weather(a) {
super(a); }public Weather(Integer id, String city, String tianqi, double temp,
String img) {
super(a);this.id = id;
this.city = city;
this.tianqi = tianqi;
this.temp = temp;
this.img = img;
}
/ * * *@return the id
*/
public Integer getId(a) {
return id;
}
/ * * *@param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/ * * *@return the city
*/
public String getCity(a) {
return city;
}
/ * * *@param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/ * * *@return the tianqi
*/
public String getTianqi(a) {
return tianqi;
}
/ * * *@param tianqi the tianqi to set
*/
public void setTianqi(String tianqi) {
this.tianqi = tianqi;
}
/ * * *@return the temp
*/
public double getTemp(a) {
return temp;
}
/ * * *@param temp the temp to set
*/
public void setTemp(double temp) {
this.temp = temp;
}
/ * * *@return the img
*/
public String getImg(a) {
return img;
}
/ * * *@param img the img to set
*/
public void setImg(String img) {
this.img = img; }}Copy the code
4, MainActivity. Java
package com.t20.weather;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.t20.weather.entity.Weather;
import com.t20.weather.view.RefreshListView;
import com.t20.weather.view.RefreshListView.OnRefreshListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private RefreshListView lvWeather;
private List<Weather> weaList;
private MyAdapter myAdapter;
private int pageIndex=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvWeather=(RefreshListView) findViewById(R.id.lvWeather);
// Use xUtils to load background data into the list set
final HttpUtils hu=new HttpUtils();
String url="Http://172.168.40.97:8088/android0/WeatherServlet? pageIndex="+pageIndex;
hu.send(HttpMethod.GET, url, new RequestCallBack<String>(){
@Override
public void onFailure(HttpException error, String msg) {
// TODO Auto-generated method stub
Log.e("XUtils error message", msg);
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// TODO Auto-generated method stub
String json=responseInfo.result;
// Use gson to read the contents of json
Gson gson=new Gson();
weaList=gson.fromJson(json, new TypeToken<List<Weather>>(){}.getType());
// Set the adapter
myAdapter=new MyAdapter();
lvWeather.setAdapter(myAdapter);
pageIndex++;
// Listen on the RefreshListView control
lvWeather.setOnRefreshListener(new OnRefreshListener() {
@Override
public void RefeshData(a) {
// TODO Auto-generated method stub
// Use xUtils to load background data into the list set
String url="Http://172.168.40.97:8088/android0/WeatherServlet? pageIndex="+pageIndex;
hu.send(HttpMethod.GET, url, new RequestCallBack<String>(){
@Override
public void onFailure(HttpException error, String msg) {
// TODO Auto-generated method stub
Log.e("XUtils error message", msg);
Toast.makeText(MainActivity.this."Network exception",Toast.LENGTH_SHORT).show();
lvWeather.loadFinish();
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// TODO Auto-generated method stub
String ret=responseInfo.result;
// Check if there is any data left
if(ret.equals("no")){
Toast.makeText(MainActivity.this."There's no more data.",Toast.LENGTH_SHORT).show();
}else{
// Use gson to read the contents of json
Gson gson=new Gson();
// Use temporary collections to receive data
List<Weather> tempList=gson.fromJson(ret, new TypeToken<List<Weather>>(){}.getType());
// Add the temporary set to the weather set
weaList.addAll(tempList);
// Let the adapter tell ListView to refresh the datamyAdapter.notifyDataSetChanged(); pageIndex++; } lvWeather.loadFinish(); }}); }}); }}); }class MyAdapter extends BaseAdapter{
@Override
public int getCount(a) {
// TODO Auto-generated method stub
return weaList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
// load the list_item.xml layout file
view=View.inflate(MainActivity.this, R.layout.list_item, null);
/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- context -- -- -- -- -- -- -- -- -- -- -- -- - the layout of the need to load - typically use null
//2. Get the control in the list_item.xml layout file
/*TextView tvId=(TextView) view.findViewById(R.id.tvId); * /
TextView tvCity=(TextView) view.findViewById(R.id.tvCity);
TextView tvTianqi=(TextView) view.findViewById(R.id.tvTianqi);
TextView tvTemp=(TextView) view.findViewById(R.id.tvTemp);
ImageView ivImg=(ImageView) view.findViewById(R.id.ivImg);
//3. Set the control value
Weather weather=weaList.get(position);
/*tvId.setText(weather.getId()+""); * /
tvCity.setText(weather.getCity());
tvTianqi.setText(weather.getTianqi());
tvTemp.setText(weather.getTemp()+"");
// Display network image
BitmapUtils bu=new BitmapUtils(MainActivity.this);
String uri=weather.getImg();
bu.display(ivImg, uri);
returnview; }}}Copy the code
\
\