gpt4 book ai didi

Android TextView 文本在运行时不更新

转载 作者:太空狗 更新时间:2023-10-29 15:24:11 26 4
gpt4 key购买 nike

因为更新 TextView 应该是一件容易的事,所以我必须遗漏一些完全愚蠢的东西。我有一个 Activity 类,它是另一个接收消息的观察者。在我的 Activity 的 onCreate 中,我执行以下操作并且工作正常。

theStatus = (TextView) findViewById(R.id.theStatus);
theStatus.setText("waiting");

稍后当消息 Activity 收到一条新消息时,它会将其传递给它的观察者(我的 Activity)

public void statusUpdate( GenericMessage aMessage )
{
String statusText = aMessage.getDetails();
theStatus.setText(statusText);

theSessionStatus.invalidate(); // I've tried with and without this call
}

但是屏幕没有更新。我一定是忽略了什么...

最佳答案

看起来我找到了一个解决方案,尽管感觉像是一个 hack。我从你上面的评论和 here 得到了这个想法.

我现在使用 Handler 通知 Activity B 收到了它感兴趣的消息。B 在其 onCreate 中创建了一个 Handler:

  Handler statusHandler = new Handler();
statusHandler.post(new Runnable()
{
public void run()
{
statusUpdate();
}
});

我更新了消息接收器类 C(B 试图成为其观察者)以在 B 向其注册时接收句柄。然后在 C 中,当适用的消息出现时,我调用以下命令:

theMsgObserverHandle.post(new Runnable()
{
public void run()
{
Log.d(myName, "Calling my Observer statusUpdate");

theMsgObserver.statusUpdate();
}
});

我不喜欢的是现在我不能直接将消息传递给 B。 (我的消息类是我自己定义的类,而不是 android.os.Message)现在在 B 中我需要向 C 的实例(C 是单例)询问消息。但至少我在前进。如果有人有更好的实现,我将非常感谢您的反馈。

关于Android TextView 文本在运行时不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3638891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com