Updating text for multiple instances of a TextView from a CountDownTimer

房东的猫 提交于 2019-12-12 04:32:38

问题


I am having an issue when attempting to update a TextView's text.

I have a MainActivity in which i have created a layout with an empty ListView. To fill this ListView with custom ListItems i have created a custom AsyncTaskLoader that retrieves information to fill onto the ListItems and also a custom BaseAdapter that fills the data loaded into the ListItems.

This part has no errors and it does it just perfectly.

However, i am having an issue when attempting to update the text on a TextView from a ListItem in the ListView. You see, i have created a custom CountDownTimer which takes an interface called OnTickListener as parameter in the constructor. I have a unique id for each time i created a ListItem and i am using this to store the TextView for that ListItem in a HashMap and also to store the CountDownTimer in a HashMap. These HashMaps are created and kept inside the custom BaseAdapter.

The BaseAdapter also implements OnTickListener.

Every time a MyCountDownTimer calls OnTick it calls the OnTickListener provided to it in the constructor and passes two parameters: int id and String text.

This will call the BaseAdapters implementaton of OnTickListener's only method: public void OnTick(int id, String text).

In the onTick method i do the following:

  1. Find the TextView for the id in the HashMap i mentioned earlier.
  2. Update the TextViews text.

Now this seems to work in some way. Because if i scroll up and down i can see that the text on the TextViews will be updated once it is shown on the screen again. But it does not update the TextView if it is already shown on the screen. I have tried to call TextView.invalidate() and TextView.postInvalidate(). In my desperation i even tried creating a Handler that would post a Runnable which would try to update the TextView.

But nothing seems to be working. Got any ideas as to how i may be able to update the TextView?

TL DR: I need to know how i can update a TextView inside a ListItem - that is part of an Activity - from a CountDownTimer created in a BaseAdapter.

Sorry for the wall of text ^^"


回答1:


You should update the 'data'of the adapter,that data should contain the content of each item that will be displayed on each textview.

Then call notifydatasetchanged() on the adaprer.



来源:https://stackoverflow.com/questions/13193296/updating-text-for-multiple-instances-of-a-textview-from-a-countdowntimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!