Rogue
Rogue

Reputation: 779

ListView duplicate content after notifyDataSetChanged

I'm designing a ListView powered by a custom adapter. In a fragment, i assign the adapter in onActivityCreated() like this :

private List<Datas> values = new ArrayList<Datas>();
private MyAdapter adapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    adapter = new MyAdapter(values, getActivity());
    listView.setAdapter(adapter);
    .....}

Later in the app, i load datas from internet, and i put my values like this :

while(loop on every Datas items) {
    values.add(new Datas(...));
}
adapter.notifyDataSetChange();

When the user scrolls down, i load more items (thanks to an OnScrollListener, for the record), so the previous snippet is called again. Basically, before update from scrolling, i have something like this : A, B, C, D, E, F. After update, i have A, B, C, D E, F, A, B, C, D E, F, G, H, I, J, K ...

The first values appears twice, instate of just append at the end.

Thanks for reading and help :)

Edit : I re-considered my class, with a new adapter and everything. Now, without rational explanation, it works. Thanks anyway ;)

Upvotes: 1

Views: 1151

Answers (1)

GaRRaPeTa
GaRRaPeTa

Reputation: 5598

that's because you are downloading already existing values and adding them again... you should clear the list before adding, or (better) avoid downloading data that is already present in the list

Upvotes: 1

Related Questions