返回活动后,Android ListView不显示传入消息

时间:2015-04-17 11:33:50

标签: android listview message openfire smack

即使我遇到问题,我也想创建一个聊天客户端。当我第一次打开应用程序并打开聊天(ListView)时,它可以很好地工作。

enter image description here

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

但是当我离开后面的聊天时,我想回到窗口它没有显示收到的消息但是显示了地雷

enter image description here

我发送“秒”之后,其他用户发送了:

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;
        }

我正在测试整天,就像当我回去并且我打开一个新的活动时,聊天监听器会增加预览而不是新的预览,我认为这是因为runOnUiThread。

1 个答案:

答案 0 :(得分:2)

固定这个,天啊!经过几个小时的调试XD后,我需要关闭监听器中的聊天

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

        }  
    });

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