【发布时间】:2015-03-25 07:09:00
【问题描述】:
我正在构建一个 android 应用程序,其中有一个主屏幕,其中 onCreate 我正在初始化异步任务。在那个异步任务中,我从服务器获取数据并使用 baseAdapter 显示。
现在的问题是刷新时我再次触发异步任务并且它复制了我的数据所以,我需要使用 BaseAdapter 删除旧数据并在列表中插入新数据。
这里是做异步任务 -
/**
* Async task class to get json by making HTTP call
*/
private class questionfeed_async extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
ServiceHandler http = new ServiceHandler();
jsonString = http.makeServiceCall(url, ServiceHandler.GET,
null);
if (jsonString != null) {
try {
SCROLL_TO_POSITION = currentQuestionArray.length();
questions = new JSONArray(jsonString);
//result_text = new String[questions.length()];
for (int i = 0; i < questions.length(); i++) {
temp_obj = questions.getJSONObject(i);
JSONObject currentQuestionObject = new JSONObject();
currentQuestionObject.put("question_text", temp_obj.getString(Tag_questionText)
.toString());
currentQuestionArray.put(currentQuestionObject);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.d("TAG", "Empty");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter = new BaseAdapterFeed(getActivity(), currentQuestionArray);
questionfeed_customelistview.setAdapter(adapter);
} catch (NullPointerException e) {
// probably don't bother doing clean up
} finally {
// carry on as if nothing went wrong
}
}
【问题讨论】:
标签: android asynchronous pull-to-refresh