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??

有帮助吗?

解决方案 2

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

其他提示

If the items in the list have stable ids you can loop through the adapter using getItemId(int position) and then remove the item with the correct id. Otherwise you need to track how many items were added at the top to offset to the correct position. So you could store in the UpdateTimerTask the total number of items in the list when it was created. Subtract from the total number of items in the list when it fires and you should have the position of the item.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top