关闭

griadview刷新

33人阅读 评论(0) 收藏 举报

public class MyAsyncTask extends AsyncTask<String, Void, String>{
Context context;
PullToRefreshGridView gv;
ArrayList<Contents> list=null;
String id;
private MybaseAdapter adp;

String url="http://apis.juhe.cn/goodbook/query?key=1da2300502c151c9e787ec9a63f0c178";
int start=0;
int end=3;
public MyAsyncTask(Context context, PullToRefreshGridView gv, String id){
this.context=context;
this.gv=gv;
this.id=id;
list=new ArrayList<Contents>();
}


@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getNetData(params[0]);
}


@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
JSONObject obj00=new JSONObject(result);

JSONObject obj11=obj00.getJSONObject("result");
JSONArray arr=obj11.getJSONArray("data");
for (int i = 0; i < arr.length(); i++) {
JSONObject obj=arr.getJSONObject(i);

list.add(new Contents(obj.getString("img"), obj.getString("title")));
}



//判断头部
showListView(list);
gv.onRefreshComplete();
gv.setMode(Mode.BOTH);
gv.setOnRefreshListener(new OnRefreshListener2<GridView>() {


@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
end+=3;
new AsyncTask<String, Void, String>(){


@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getNetData(params[0]);
}
protected void onPostExecute(String result) {
try {
JSONObject obj00=new JSONObject(result);

JSONObject obj11=obj00.getJSONObject("result");
JSONArray arr=obj11.getJSONArray("data");
for (int i = 0; i < arr.length(); i++) {
JSONObject obj=arr.getJSONObject(i);

list.add(0,new Contents(obj.getString("img"), obj.getString("title")));
}


showListView(list);
Toast.makeText(context, "加载到三条数据!", 0).show();
gv.onRefreshComplete();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.execute(url+"&catalog_id="+id+"&pn="+end+"&rn="+(end+3));


}


@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
end+=3;
new AsyncTask<String, Void, String>(){


@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return getNetData(params[0]);
}
protected void onPostExecute(String result) {
try {
JSONObject obj00=new JSONObject(result);

JSONObject obj11=obj00.getJSONObject("result");
JSONArray arr=obj11.getJSONArray("data");
for (int i = 0; i < arr.length(); i++) {
JSONObject obj=arr.getJSONObject(i);

list.add(new Contents(obj.getString("img"), obj.getString("title")));
}


showListView(list);
Toast.makeText(context, "加载到三条数据!", 0).show();
gv.onRefreshComplete();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.execute(url+"&catalog_id="+id+"&pn="+end+"&rn="+(end+3));


}
});


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

protected String getNetData(String ss) {
HttpURLConnection conn = null; //连接对象  
        InputStream is = null;  
String resultData = "";  
       try {  
           URL url = new URL(ss); //URL对象  
           conn = (HttpURLConnection)url.openConnection(); //使用URL打开一个链接  
           conn.setDoInput(true); //允许输入流,即允许下载  
           conn.setDoOutput(true); //允许输出流,即允许上传  
           conn.setUseCaches(false); //不使用缓冲  
           conn.setRequestMethod("GET"); //使用get请求  
           is = conn.getInputStream();   //获取输入流,此时才真正建立链接  
           InputStreamReader isr = new InputStreamReader(is);  
           BufferedReader bufferReader = new BufferedReader(isr);  
           String inputLine  = "";  
           while((inputLine = bufferReader.readLine()) != null){  
               resultData += inputLine + "\n";  
           }  
 
       } catch (MalformedURLException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }catch (IOException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }
       return resultData;  
}

private void showListView(ArrayList<Contents> bblist) {
if (adp == null) {
adp=new MybaseAdapter(context,bblist);
gv.setAdapter(adp);
} else {
adp.notifyDataSetChanged();
}
}
/*gv.setOnRefreshListener(new OnRefreshListener<GridView>() {

@Override
public void onRefresh(PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
if(gv.isHeader()){
end+=3;
new MyAsyncTask(context, gv,null).execute(url+"&catalog_id="+id+"&pn="+start+"&rn="+end);

String label = DateUtils.formatDateTime(  
                    context.getApplicationContext(),  
                    System.currentTimeMillis(),  
                    DateUtils.FORMAT_SHOW_TIME  
                            | DateUtils.FORMAT_SHOW_DATE  
                            | DateUtils.FORMAT_ABBREV_ALL);  
            // 显示最后更新的时间  
            refreshView.getLoadingLayoutProxy()  
                    .setLastUpdatedLabel(label);

showListView(list);
Toast.makeText(context, "加载到三条数据!", 0).show();
gv.onRefreshComplete();
}
if(gv.isFooter()){
new MyAsyncTask(context, gv,null).execute(url+"&catalog_id="+id+"&pn="+start+"&rn="+end);

showListView(list);
Toast.makeText(context, "加载到三条数据!", 0).show();
gv.onRefreshComplete();
}
}
});*/
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:860次
    • 积分:86
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档