Search code examples
androidlistviewandroid-listviewandroid-adapterandroid-adapterview

How can I remove listview items after a few seconds?


How can I add a timer with every item of the listview? I want the row to disappear after two seconds. The list contains only strings

I currently have a handler in my adapter with handler.postDelayed(new UpdateTimerTask(), 2000);

inside the getView method.

This task implements a Runnable that should remove an item from the list and call notifyDataSetChanged().

But I cannot call remove with a position because elements are constantly added at the top in my case. The strings could also be the same and hence i cannot remove by name.

Any ideas??


Solution

  • You need to use a custom model for your list items, say StringItem, which has both a String field to store the text and an integer equal to System.currentTimeMillis() (at the time of their creation) to keep track of their lifetimes. Now on a small interval like every 100 milliseconds do a cleanup on the list based on current value of System.currentTimeMillis() and the life time id of each item in the list