Refreshing listview duplicates content

3.9k views Asked by At

I have a listview which populated with arraylist. However, anytime I use the pull to refresh, the data in the listview gets duplication even though I clear the arraylist before populating. This is my arraylist

List<TimeItems> myList = new ArrayList<TimeItems>();

and this is how I am populating the adapter

for (int in = 0; in <= video.size() - 1; in ++) {
                    String a = vDancers.get(in);
                    String b = video.get(in);
                    String c = vName.get(in);

                    TimeLineItems item = new TimeLineItems(a,b,c);
                    myList.add(item);
                }

                Collections.sort(myList);
                you = new MyVideoAdapter(getActivity(), myList,nLikes,nComments,nRepost);
                //you.notifyDataSetChanged();
                listView.setAdapter(you);

and this is how I am clearing myList on pull-to-refresh

 listView.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    you = null;
                    nComments.clear();
                    nRepost.clear();
                    nRepost.clear();
                    myList.clear();

                    parseFetch();
                    listView.onRefreshComplete();
                }
            }, 5000);
        }
    });

where you is my BaseAdapter and parseFetch() is my method to load network data. How do I completely clear myList before refresh so I don't have duplicated in my listview? Thanks for your help

3

There are 3 answers

0
saintjab On BEST ANSWER

I got it. I wasn't also clearing the other arrays I was using to populate myList. After logging the various stages, I realised these arrays were duplicated so I just needed to clear them as well. This is the final work.Thanks @Lavkush2oct and @Vesko for the help.

listView.setOnRefreshListener(new OnRefreshListener() {
    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                you = null;
                nComments.clear();
                nRepost.clear();
                nRepost.clear();
                myList.clear();
                vDancers.clear();
                video.clear();
                vName.clear();
                you.notifyDataSetChanged();
                parseFetch();
                listView.onRefreshComplete();
            }
        }, 5000);
    }
});
3
Lavakush On

Call notifyDataSetChanged() on your list adapter. Remove this line you = null;

myList.clear(); you.notifyDataSetChanged();

1
Vesko On

Based on the code snippets I see, I'm left with the impression you're using an ArrayAdapter. If that's really.the case why do you manipulate myList after your adapter you is initialised?

All you can do is call

you.clear()
// add your data with you.add(item)