android 异步加载 二

原创 2013年12月06日 11:53:31

          内部类需继承 AsyncTask  同时必须 Override      doInBackground  和   onPostExecute

       

             

private class GetDataTask extends AsyncTask<Void, Void, List<Problem>> {
private int type;
private int rangetype;
private int page;


public GetDataTask(int type, int rangetype) {
super();
this.type = type;
this.rangetype = rangetype;
}


@Override
protected List<Problem> doInBackground(Void... params) {
switch (type) {
case 0:
page_all++;
page = page_all;
break;
case 1:
page_unanswer++;
page = page_unanswer;
break;
case 2:
page_answer++;
page = page_answer;
break;
case 3:
page_accept++;
page = page_accept;
break;
}


RequestVo requestVo = new RequestVo();
requestVo.context = DisabuseActivity.this;
requestVo.requestUrl = Constants.FINDPROBLEM_URL;
requestVo.requestDataMap = new HashMap<String, String>();
requestVo.requestDataMap.put("keywords", keywords);
requestVo.requestDataMap.put("type", String.valueOf(type));
requestVo.requestDataMap.put("rangetype", String.valueOf(rangetype));
requestVo.requestDataMap.put("page", String.valueOf(page));


requestVo.requestDataMap.put("num", String.valueOf(10));


String data = (String) NetUtil.post(requestVo);


List<Problem> problemList = null;
try {
JSONArray jsonArray = new JSONArray(data);


JSONObject lastpJsonObject = jsonArray.getJSONObject(jsonArray.length() - 2);
int lastp = lastpJsonObject.getInt("lastp");
switch (type) {
case 0:
totalpage_all = lastp;
break;
case 1:
totalpage_unanswer = lastp;
break;
case 2:
totalpage_answer = lastp;
break;
case 3:
totalpage_accept = lastp;
break;
}


problemList = new ArrayList<Problem>();
for (int i = 0; i < jsonArray.length() - 2; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Problem problem = new Problem();
String pid = jsonObject.getString("pid");
String userid = jsonObject.getString("uid");
String click = jsonObject.getString("click");
String name = jsonObject.getString("name");
String headimg = jsonObject.getString("photo");
int type = jsonObject.getInt("type");
int status = jsonObject.getInt("status");
problem.setPid(pid);
problem.setUserid(userid);
problem.setType(type);
problem.setStatus(status);
problem.setClick(click);
problem.setHeadimg(headimg);
problem.setName(name);
switch (status) {
case 0:
String unanswer_problem = jsonObject.getString("problem");
String unanswer_createtime = jsonObject.getString("createtime");
problem.setUnanswer_createtime(unanswer_createtime);
problem.setUnanswer_problem(unanswer_problem);
break;
case 1:
case 3:
String answered_problem = jsonObject.getString("problem");
String answered_createtime = jsonObject.getString("createtime");
String answered_answer = jsonObject.getString("answer");
problem.setAnswered_answer(answered_answer);
problem.setAnswered_problem(answered_problem);
problem.setAnswered_createtime(answered_createtime);
break;
case 2:
String accept_problem = jsonObject.getString("problem");
String accept_createtime = jsonObject.getString("createtime");
String accept_answer = jsonObject.getString("answer");
problem.setAccept_answer(accept_answer);
problem.setAccept_createtime(accept_createtime);
problem.setAccept_problem(accept_problem);
break;
}
problemList.add(problem);
}
} catch (JSONException e) {
listViewHandler.sendEmptyMessage(0);
}
return problemList;

}


         @Override
protected void onPostExecute(List<Problem> result) {
super.onPostExecute(result);
if (result == null) {
return;
}
switch (type) {
case 0:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_unanswer:" + page_unanswer);
list_all.addAll(result);
allAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_all.onRefreshComplete();
if (page_all >= totalpage_all) {
findproblem_problemlisting_listview_all.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_all.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 1:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_answer:" + page_answer);
list_unanswer.addAll(result);
unanswerAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_unanswer.onRefreshComplete();
if (page_unanswer >= totalpage_unanswer) {
findproblem_problemlisting_listview_unanswer.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_unanswer.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 2:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_accept:" + page_accept);
list_answer.addAll(result);
answerAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_answer.onRefreshComplete();
if (page_answer >= totalpage_answer) {
findproblem_problemlisting_listview_answer.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_answer.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 3:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_accept:" + page_accept);
list_accept.addAll(result);
acceptAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_accept.onRefreshComplete();
if (page_accept >= totalpage_accept) {
findproblem_problemlisting_listview_accept.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_accept.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
}
findproblem_data_loading.setVisibility(View.GONE);
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

android的弹出框 -(一)--AlertDialog,时间弹出框DatePickerDialog 和进度条弹出框showProgressDialog

这里介绍三种弹出框android的弹出框AlertDialog,时间弹出框DatePickerDialog 和进度条弹出框showProgressDialog--我将在下一篇介绍自定义弹出框 ...

Android中数据提交工具类(参考)

NetUtil.java package com.gzucm.volunteer.util; import java.io.IOException; import java.util.ArrayL...

android图片异步加载

  • 2014年09月25日 14:41
  • 1.28MB
  • 下载

Android异步加载(二)

上篇我们已经获取到了标题和内容,这篇主要讲解获取图片的俩种方式 一实现效果: 二.使用Thread加载图片,代码如下: 子线程不能更新UI, 使用handler处理,将bitmap以消息的形式发...

Android异步图片加载

  • 2013年05月24日 17:28
  • 2.85MB
  • 下载

android 图片异步加载

  • 2013年11月25日 15:19
  • 1005KB
  • 下载

打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(二)ORM注解以及解析

前面提到了我使用注解做ORM映射 1.首先是绑定item布局文件的注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) ...

android异步加载网络图片到ListView

  • 2011年01月13日 12:33
  • 54KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android 异步加载 二
举报原因:
原因补充:

(最多只允许输入30个字)