I have a Custom ListView in my application, containing an ImageView and 3-5 TextViews.
1 of the TextViews shows the time gap between the current time and the time specified for that ListView item.
Like: Posted 5 minutes ago
The question is, how can i update this TextView every minute without any disturbance to the ListView.
Like: if i am looking at the ListView for 5 minutes, just this TextView alone must change from 1 min to 2 mins so on for every item visible
What i tried:
- set this TextView in getView() using
System.currentTimeMillis() - givenTime[position];
but here the TextView won’t get updated if i am not scrolling (not calling the getView) or changing the visibility of the items.
- Use a timeTask() thread every 1 minute and try to update the TextView of every item
here the problem is i get an Exception:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
moreover i have no idea how to change the TextView for every ListView item from the Activity.
But this looks like a familiar case.
How can i achieve this?
Thank You
Use a
Handlerand itspostDelayedmethod to invalidate the list’s adapter as follows:You must only update UI in the main (UI) thread.
By creating the handler in the main thread, you ensure that everything you post to the handler is run in the main thread also.