Android ListView 返回 activity 后不显示收到的消息

Android ListView not displaying incomming messages after returning to the activity

我正在尝试创建一个聊天客户端,尽管我遇到了问题。当我第一次打开应用程序并打开聊天 (ListView) 时,它运行良好。

04-17 07:19:51.681  10020-10081/com.example.example E/SENDER ANSWER:﹕ yes
04-17 07:19:55.494  10020-10081/com.example.example E/SENDER ANSWER:﹕ hi

但是当我用后退按钮离开聊天时我想返回 window 它不显示收到的消息但显示地雷

在我发送 "second" 之后,其他用户发送:

04-17 07:21:17.246  10020-10081/com.example.example E/SENDER ANSWER:﹕ second time and doesnt work

并且它没有显示在列表视图中。 这是我的代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    chatmanager.addChatListener(new ChatManagerListener() {
                @Override
                public void chatCreated(Chat chat, boolean createdLocally) {
                if (!createdLocally) {
                    chat.addMessageListener(new ChatMessageListener() {
                        @Override
                        public void processMessage(Chat chat, Message message) {
                            Log.e("SENDER ANSWER:", message.getBody());

                            sender_messages = message.getBody();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    populateConversation(sender_messages, true);

                                    populateConversationView();
                                }
                            });
                        }

                    });
                }

                }
            });
}

也许它不必与聊天侦听器一起使用,因为它正在接收消息但只是不显示。我将不胜感激

public void populateConversationView(){
 final ArrayAdapter<Message_Provider> adapter = new MyListAdapter();


messagesList.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    messagesList.setAdapter(adapter);

    adapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            messagesList.setSelection(adapter.getCount());
        }
    });

 private class MyListAdapter extends ArrayAdapter<Message_Provider> { // Done
        public MyListAdapter(){
            super(getApplicationContext(), R.layout.chat_conversation, messages);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if(itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.source_message, parent, false);
            }


            Message_Provider currentContact = messages.get(position);

            TextView myMessage = (TextView) itemView.findViewById(R.id.senderMessege);

            myMessage.setText(currentContact.getMessage());

            LinearLayout.LayoutParams parameter = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (currentContact.getSender()){
                parameter.gravity = Gravity.LEFT;
                myMessage.setBackgroundResource(R.drawable.sender_messages);

            }else {
                parameter.gravity = Gravity.RIGHT;
                myMessage.setBackgroundResource(R.drawable.my_message);

            }
            myMessage.setLayoutParams(parameter);


            return itemView;
        }

我测试了一整天,就像我回去打开一个新的 activity 聊天侦听器增加预览一个而不是新的,我认为这是因为 runOnUiThread .

已修复此问题,天啊!经过几个小时的调试 XD 我需要关闭监听器中的聊天

runOnUiThread(new Runnable() {
        @Override
        public void run() {
        populateConversation(sender_messages, true);
        populateConversationView();
        chat.close(); // HERE

        }  
    });

我post这是因为有像我这样的初学者,也可能遇到这个问题。