Reputation: 3177
In picture one, after selecting one item in the List, saying History, a new activity starts. Please see picture two. In picture two, after clicking going back to the List, The items in List repeat themselves, just as picture three.
Why does this happen?
The following is part of my code:
public class CategoryListActivity extends ListActivity implements LoaderCallbacks<List<Category>> {
String url = "http://vlm1.uta.edu/~zhangzhong/questions.json";
private ArrayAdapter<Category> mListAdapter;
//private ListView listview;
//private ArrayList<Category> categories = new ArrayList<Category>();
private static final int LOADER_ID_CHECKS = 1;
private static final String TAG = "CategoryListActivity";
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//listview = (ListView) findViewById(R.id.listview);
mListAdapter = new ArrayAdapter<Category>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
DataStore.categories);
//listview.setOnItemClickListener(mOnItemClickListener);
//listview.setAdapter(mListAdapter);
setListAdapter(mListAdapter);
getLoaderManager().initLoader(LOADER_ID_CHECKS, null, this);
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
Intent detailIntent = new Intent(this, ScreenSlideActivity.class);
detailIntent.putExtra("category_id", id);
startActivity(detailIntent);
}
@Override
public Loader<List<Category>> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_ID_CHECKS:
Loader<List<Category>> loader = new CategoryListLoader(this, url);
loader.forceLoad();
return loader;
}
return null;
}
@Override
public void onLoadFinished(Loader<List<Category>> loader, List<Category> result) {
Log.d(TAG, "onLoadFinished");
if (result != null) {
Log.d(TAG, "items: " + result.size());
}
switch (loader.getId()) {
case LOADER_ID_CHECKS:
if (result != null) {
for (Category category : result) {
DataStore.categories.add(category);
}
}
mListAdapter.notifyDataSetChanged();
break;
}
}
@Override
public void onLoaderReset(Loader<List<Category>> loader) {
Log.d(TAG, "onLoaderReset");
switch (loader.getId()) {
case LOADER_ID_CHECKS:
break;
}
}
}
Upvotes: 0
Views: 83
Reputation: 28866
case LOADER_ID_CHECKS:
if (result != null) {
for (Category category : result) {
DataStore.categories.add(category);
}
}
mListAdapter.notifyDataSetChanged();
break;
should be
case LOADER_ID_CHECKS:
DataStore.categories.clear();
if (result != null) {
DataStore.categories.addAll(result);
}
mListAdapter.notifyDataSetChanged();
break;
Upvotes: 1
Reputation: 7763
Check onLoadFinished() is calling again or not. If onLoadFinishe() is again called your datas will be once again added to the array list.
Upvotes: 0