关闭

android 异步加载 二

标签: android 异步加载
435人阅读 评论(0) 收藏 举报
分类:

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

0
0

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