欧阳明日的专栏

不积跬步,无以至千里

android 异步加载 二

          内部类需继承 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);
}
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaoshaolei123/article/details/17164247
文章标签: android 异步加载
个人分类: Android
上一篇Android 异步加载
下一篇懒加载ImageLoader的应用
想对作者说点什么? 我来说一句

Android异步加载网络图片二例

2011年08月19日 145KB 下载

Android异步加载-demo-mesmall

2013年12月31日 681KB 下载

android BaseAdapter AsyncTask

2014年05月11日 1.15MB 下载

android 异步加载

2011年11月26日 1.11MB 下载

listview 异步加载网络图片

2013年11月05日 76KB 下载

android异步加载网络图片实例

2011年12月26日 27KB 下载

Android Handler AsyncTask 异步加载

2014年05月11日 1.61MB 下载

android异步操作示例

2012年08月18日 1.64MB 下载

没有更多推荐了,返回首页

关闭
关闭