Android不追加()文字正确的TextView [英] Android does not append() text to textview properly

查看:93
本文介绍了Android不追加()文字正确的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有两种方式文本追加一个TextView:
第一种方式:通过点击按钮,得到什么是的EditText

I have a textview in which i append text in two ways: First way: by clicking button and getting what is in EditText.

 sendbutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              txview.append("Text: "+txedit.getText()+"\n");
              txedit.setText("");

 }
          });

这工作得很好,当我点击按钮的TextView更新为新文本视图。

And this works fine, when i click the button textview updates the view with new text.

BUT

第二种方式,我听XMPP监听器(asmack库)和recieving消息我就追加到TextView的。

The second way, I listen to xmpp listener (asmack library) and on recieving message I append it to textView.

 chat = xmpp.getChatManager().createChat(contactid[1], new MessageListener() {
                public void processMessage(Chat chat, Message message) {    
                     try {
                        chat.sendMessage(message.getBody());
                    } catch (XMPPException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                txview.append("Text"+message.getFrom()+"\n"+message.getBody()+"\n");               }                                                       }
            );;

问题是,我收到消息,我知道它的肯定(因为我把它重新发送与chat.sendMessage(message.getBody())的用户; ), BUT 的TextView显示后,才更改视图或获取应用程序的背景(点击home键),然后得到它回到前面我的消息。

The Problem is, that i recieve message, i know it for sure (because i resend it to user with chat.sendMessage(message.getBody());) , BUT textview shows my messages only after I change the view or get application to background (clicking home button) and then getting it back to the front.

我试过
调用无效()在每一个视图中,并没有在所有的工作。

What I tried calling invalidate() on every view, doesn't work at all.

这是任何解决方案或其他的方式做做什么IM?

Is it any solution or an other way to do what im doing?

推荐答案

您可以实现通过做下面的技巧张贴在GUI线程。
通过上下文(的活动或服务),以你的听众。内部监听器:

You can achieve posting on GUI thread by doing following trick. Pass your context (Activity or Service) to your listener. Inside listener:

Handler h = new Handler(context.getMainLooper());

h.post(new Runnable() {
    @Override
    public void run() {
         txview.append("Text"+message.getFrom()+"\n"+message.getBody()+"\n"); 
    }
});

这篇关于Android不追加()文字正确的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆