刷新数据
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.GridView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.zcp.adapter.GApapter;
import com.zcp.bean.Conn;
import com.zcp.bean.Data;
import com.zcp.bean.Good;
import com.zcp.bean.Myresult;
import com.zcp.bean.Myrows;
import com.zcp.bean.result;
public class IndexActivity extends Activity {
private List<Myrows> d;
private Intent in;
private String id;
private HttpResponse res;
private HttpEntity en;
private String s;
String uri="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0";
private Handler h;
private List<Myrows> list=new ArrayList<Myrows>();
private List<Myrows> list1=new ArrayList<Myrows>();
private boolean flag=true;
int j=20;
private PullToRefreshGridView pull;
private GridView grid;
private GApapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
in=getIntent();
id=in.getStringExtra("id");
pull=(PullToRefreshGridView)findViewById(R.id.pull_refresh_grid);
grid=pull.getRefreshableView();
init();
pull.setOnRefreshListener(new OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
flag=true;
init();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
flag=false;
init();
}
});
adapter=new GApapter(list,IndexActivity.this);
grid.setAdapter(adapter);
}
private void init() {
new Thread(){
public void run() {
httpUtil(uri);
};
}.start();
h=new Handler(){
public void handleMessage(android.os.Message msg) {
String s=(String) msg.obj;
Gson gson=new Gson();
Good c=gson.fromJson(s,Good.class);
Myresult r = c.getResult();
d=r.getRows();
if(flag){
listUtil(d);
}else{
listUtil01(d);
}
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
};
};
}
public void httpUtil(String uri){
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(uri);
try {
res=client.execute(get);
int i=res.getStatusLine().getStatusCode();
if(i==200){
en=res.getEntity();
s=EntityUtils.toString(en,"utf-8");
Message message=Message.obtain();
message.obj=s;
h.sendMessage(message);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void listUtil(List<Myrows> d){
list.clear();
for (int i = 0; i <d.size(); i++) {
if(i<10){
list.add(d.get(i));
}else{
list1.add(d.get(i));
}
}
}
public void listUtil01(List<Myrows> d){
list.clear();
for (int i =0; i <j; i++) {
if(i==d.size()){
Toast.makeText(IndexActivity.this,"已经是最后一条了",1).show();
return;
}
list.add(d.get(i));
}
j+=10;
}
}
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="1"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="@drawable/a" />