这是我的问题。我有一个AsyncTask的代码:
private class BackgroundAsyncTask extends AsyncTask<String, Void, Integer> {
OkHttpUtil okHttp = new OkHttpUtil();
private static final int RESULT_FAILURE = 0;
private static final int RESULT_SUCCESS = 1;
private static final int RESULT_DATA_ADDED = 2;
private static final String TAG_SUCCESS = "success";
private static final String TAG_ARRAY = "articles";
private static final String url = "http://x/db/fetch_all.php";
@Override
protected void onPreExecute() {
super.onPreExecute();
if (mSwipeRefreshLayout != null) {
mSwipeRefreshLayout.setRefreshing(true);
}
}
@Override
protected Integer doInBackground(String... params) {
int result = 0;
try {
JSONObject jsonObject = new JSONObject(okHttp.okHttpGet(url));
result = jsonObject.optInt(TAG_SUCCESS);
if (result == RESULT_SUCCESS) {
JSONArray jsonArray = jsonObject.optJSONArray(TAG_ARRAY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonArticle = jsonArray.optJSONObject(i);
int id = jsonArticle.optInt(NewsletterHContract.FavoritesColumns.ID);
String img = jsonArticle.optString(NewsletterHContract.FavoritesColumns.IMG);
String title = jsonArticle.optString(NewsletterHContract.FavoritesColumns.TITLE);
String content = jsonArticle.optString(NewsletterHContract.FavoritesColumns.CONTENT);
mArticleList.add(new Article(id, img, title, content));
result = RESULT_DATA_ADDED;
}
}
} catch (Exception e) {
Timber.e(e, "Error parsing JSON Object");
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (result == RESULT_DATA_ADDED) {
mRecyclerView.getAdapter().notifyDataSetChanged();
}
if (mSwipeRefreshLayout != null) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
AsyncTasks碰巧对我来说很新鲜。现在问题是当用户刷新时,我正在执行new BackgroundAsyncTask().execute()
。
这样做会再次将相同的内容添加到我的RecyclerView中。有什么方法可以完成这些事情:
让我提一下,我正在我片段的onCreateView()
中执行AsyncTask。
答案 0 :(得分:0)
您是否考虑过添加
来清除ArticleList@Override
protected void onPreExecute() {
super.onPreExecute();
if (mSwipeRefreshLayout != null) {
mArticleList.clear();
mSwipeRefreshLayout.setRefreshing(true);
}
}
或者如果你想添加不存在的东西你可以做
// add this function to your class
public boolean hasArticleInList(int id)
{
for(Article a : mArticleList){
if(a.getId == id){return true;}
}
return false;
}
@Override
protected Integer doInBackground(String... params) {
int result = 0;
try {
JSONObject jsonObject = new JSONObject(okHttp.okHttpGet(url));
result = jsonObject.optInt(TAG_SUCCESS);
if (result == RESULT_SUCCESS) {
JSONArray jsonArray = jsonObject.optJSONArray(TAG_ARRAY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonArticle = jsonArray.optJSONObject(i);
int id = jsonArticle.optInt(NewsletterHContract.FavoritesColumns.ID);
String img = jsonArticle.optString(NewsletterHContract.FavoritesColumns.IMG);
String title = jsonArticle.optString(NewsletterHContract.FavoritesColumns.TITLE);
String content = jsonArticle.optString(NewsletterHContract.FavoritesColumns.CONTENT);
//CHANGE THIS BIT TO
if(!hasArticleInList(id)){mArticleList.add(new Article(id, img, title, content));}
result = RESULT_DATA_ADDED;
}
}
} catch (Exception e) {
Timber.e(e, "Error parsing JSON Object");
}
return result;
}
要防止异步任务在方向更改上运行,您有许多选项,要么您可以检查您的ArrayList是否为null,以便在创建视图上调用它(但仅限第一次),或者您可以创建一个名为first的布尔值并将其初始化为false; 然后在第一次同步后,您可以将其切换为true并将调用包装到if块中的异步类