简体   繁体   中英

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. 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. 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.

    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. So in your code above, you could modify your processMessage() method like this:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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