Sidaoui Majdi
Sidaoui Majdi

Reputation: 419

remove items from listView android

I have a ListView that contains list of events, when i scroll the list i load more items and always i souldn't have more than 20 events visible in my screen. So i proceed like this:

    //load data from server and add it to my eventsList
    int size = eventsList.size();
    List<Data> temp;
    if(size>20) {
        eventsList = eventsList.subList(size - 20, size);
    }

    Log.e("eventsList Load More", " " + eventsList.size() + " " + principalSchedule.getCount());

    //Tell to the adapter that changes have been made, this will cause the list to refresh
    principalSchedule.notifyDataSetChanged();

but the problem i always have eventsList.size() = 20 and principalSchedule.getCount() does't decrease.

Upvotes: 0

Views: 241

Answers (2)

Dale Wilson
Dale Wilson

Reputation: 9434

Your ListView should be interacting with your list via a ViewAdapter (probably an ArrayAdapter but your haven't shown that code) If you create your own Adapter (not very hard to do) then the getView() method on that adapter will be able to determine when an item is no longer visible and possibly reclaim the resources.

This link: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ goes into greater detail about one technique for an adapter that loads resources in a background thread and can free the resources when they are no longer needed. It doesn't exactly match your use case but if you read it and understand it you should see how to write the code you need for your case.

Upvotes: 0

Hugo Hideki Yamashita
Hugo Hideki Yamashita

Reputation: 221

List<E>#subList(int, int) method returns a new List<E> with the subset of the initial List and doesn't change the original List.

If you aren't setting eventList back to the principalSchedule, the List from prin

Upvotes: 1

Related Questions