简体   繁体   English

Android TextView 不更新视图

[英]Android TextView doesn't update the view

So I have a textview in which I tv.append some strings when my listener get's some specific data.所以我有一个 textview ,当我的听众得到一些特定数据时,我在其中 tv.append 一些字符串。 The problem is, that textview updates the text only when I move to another view or hide app in the background and then get it back to the front.问题是,textview 仅在我移动到另一个视图或在后台隐藏应用程序然后将其放回前面时才更新文本。 So basicaly i make a textview, Then I have a listener based on asmack library (XMPP) When i recieve a messgae I append it to textview, but i can see it only by moving to another view and going back.所以基本上我做了一个textview,然后我有一个基于asmack库(XMPP)的监听器当我收到一个消息时我append它到ZE62B85E9C91FF5C83D5CC8D8EB9F11,但只能通过移动到另一个查看。

    tv = (TextView)findViewById(R.id.textwindow);

chat = xmpp.getChatManager().createChat(contactid, new MessageListener() {
                public void processMessage(Chat chat, Message message) {                  
                   System.out.println("Reciveved:"+ message);
                 tv.append("From:"+chat.getParticipant()+"\n"+message.getBody()+"\n");

                }                                       
                    }
            );;

You can force a redraw by calling the invalidate() method on either the View or its containing ViewGroup.您可以通过在 View 或其包含的 ViewGroup 上调用 invalidate() 方法来强制重绘。 So in your code above, you could modify your processMessage() method like this:因此,在上面的代码中,您可以像这样修改您的 processMessage() 方法:

public void processMessage(Chat chat, Message message) {                  
  System.out.println("Reciveved:"+ message);
  tv.append("From:"+chat.getParticipant()+"\n"+message.getBody()+"\n");
  tv.invalidate();         
}

You might want to take a look at a related question answered here .您可能想查看此处回答的相关问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM