Android TextView text not updating during runtime
I'开发者_如何学编程ve got to be missing something completely stupid on this one since updating a TextView should be an easy task. I have an Activity class that is an observer of another that receives messages. In my Activity's onCreate I do the following and it works fine.
theStatus = (TextView) findViewById(R.id.theStatus);
theStatus.setText("waiting");
Later when the message activity receives a new message it hands it off to it's observer (my Activity)
public void statusUpdate( GenericMessage aMessage )
{
String statusText = aMessage.getDetails();
theStatus.setText(statusText);
theSessionStatus.invalidate(); // I've tried with and without this call
}
However the screen doesn't update. I must be overlooking something...
It looks like I found a solution, although it feels like a hack. I got the idea from your comments above and from here.
I am now using a Handler to notify the Activity B that a message has come in that it is interested in. B creates a Handler in its onCreate:
Handler statusHandler = new Handler();
statusHandler.post(new Runnable()
{
public void run()
{
statusUpdate();
}
});
I updated the message receiver class C (that B was trying to be an Observer of) to take in the Handle when B registers with it. Then in C, I call the following when the applicable message comes in:
theMsgObserverHandle.post(new Runnable()
{
public void run()
{
Log.d(myName, "Calling my Observer statusUpdate");
theMsgObserver.statusUpdate();
}
});
What I don't like is that now I can't just pass my message to B directly. (My message class is my own defined class not android.os.Message) Instead now in B I need to ask the instance of C (C is a Singleton) for the message. But at least I'm moving forward. If anyone has a better implementation I'd really appreciate the feedback.
精彩评论