1

I think my problem should be at adapter in processmessage() method. I don't know how to solve it, can anyone provide me a solution with coding and explain? Looking forward any reply, thanks in advance.

This is my activity code, correct me if i'm wrong

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_message);

    submitButton.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            QBChatMessage chatMessage = new QBChatMessage();
            chatMessage.setBody(edtContent.getText().toString());
            chatMessage.setSenderId(QBChatService.getInstance().getUser().getId());
            //save chat message to history
            chatMessage.setSaveToHistory(true);

            try{
                qbChatDialog.sendMessage(chatMessage);
                //Once message send out, message will be encrypt and send to CloudMQTT server
                publish();
            }catch (SmackException.NotConnectedException e){
                e.printStackTrace();
            }

            //Put message to cache
            if (qbChatDialog.getType() == QBDialogType.PRIVATE){
                QBChatMessagesHolder.getInstance().putMessage(qbChatDialog.getDialogId(),chatMessage);
                ArrayList<QBChatMessage>messages = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(chatMessage.getDialogId());

                adapter = new ChatMessageAdapter(getBaseContext(),messages);
                lstChatMessages.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            edtContent.setText("");
            edtContent.setFocusable(true);

        }
    });
}

@Override
public void processMessage(String s, QBChatMessage qbChatMessage, Integer integer) {
    //Cache message
    QBChatMessagesHolder.getInstance().putMessage(qbChatMessage.getDialogId(),qbChatMessage);
    ArrayList<QBChatMessage>messages = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(qbChatMessage.getDialogId());
    adapter = new ChatMessageAdapter(getBaseContext(),messages);
    lstChatMessages.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

This is my adapter code, correct me if i'm wrong

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //If sender then chat message layout will diplay list_send_message.xml else dislplay list_recv_message.xml file
        if (qbChatMessages.get(position).getSenderId().equals(QBChatService.getInstance().getUser().getId())){
            view = inflater.inflate(R.layout.list_send_message, null);
            BubbleTextView bubbleTextView = (BubbleTextView)view.findViewById(R.id.message_content);
            bubbleTextView.setText(qbChatMessages.get(position).getBody());
        }else {
            view = inflater.inflate(R.layout.list_recv_message, null);
            BubbleTextView bubbleTextView = (BubbleTextView)view.findViewById(R.id.message_content);
            bubbleTextView.setText(qbChatMessages.get(position).getBody());
            TextView txtName = (TextView)view.findViewById(R.id.message_user);
            txtName.setText(QBUsersHolder.getInstance().getUserById(qbChatMessages.get(position).getSenderId()).getFullName());
        }
    }else{

    }
    return view;
}
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Elson
  • 11
  • 3
  • why are you replacing the adapter on every send? Is this a text message type of app. I see you only refresh the list if it is a private message, but you replace the adapter every time. Need more details to help – Sam Dec 01 '17 at 17:36
  • @Sam Thanks for your comment. Can i have your email so i can send u my source code for troubleshoot? I replace the adapter on every send because i need update sent message in adapter – Elson Dec 02 '17 at 03:28

0 Answers0