使用Java在android中添加聊天消息后无法更新recyclerview列表

问题描述

我需要在 android 的 recyclerview 中添加消息。但是添加notifyDataSetChaged()后,却没有添加到列表中。

这是我的代码

使用过的适配器和片段。

适配器将根据用户 ID 左右显示接收者和发送者的消息。绑定持有者id,用于在左右显示自己的用户和其他用户消息的列表,与wapp相同。

聊天片段:

   public void onviewCreated() {
    
     mSendMsg.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msgContent = mSendMsginputBox.getText().toString();
    
                if(!TextUtils.isEmpty(msgContent))
                {
                    execute(new PostMessageRequest( msgContent,getmId()),mPostMessageListener);
                    int i1 = msharedprefs.getInt(msgContent,0);
                    i1 += 1;
                    SharedPreferences.Editor editor = msharedprefs.edit();
                    editor.putInt(msgContent,i1);
                    editor.commit();
                    int newMsgPosition = messagesList.size();
                    System.out.println("newMsgPosition ==" + newMsgPosition);
    
                    // Notify recycler view insert one new data.
                    mAdapter.notifyItemInserted(newMsgPosition);
                    // Scroll RecyclerView to the last message.
                    //mChatMessageRecyclerView.scrollToPosition(0);
    
                 
                    mSendMsginputBox.setText("");
                    mAdapter.notifyDataSetChanged();
    
                }
            }
        });
    
    My Adapter:


       private static final int VIEW_TYPE_MESSAGE_SENT = 1;
        private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
        private List<MyMessageModel> msgDtoList; 
        MyMessageModel msgDto = this.msgDtoList.get(position);
         

         @Override
    public int getItemViewType(int position) {
        MyMessageModel message = (MyMessageModel) msgDtoList.get(position);

        if (message.getId().equals(Profile.getId())) {
            // If the current user is the sender of the message
            return VIEW_TYPE_MESSAGE_SENT;
        } else {
            // If some other user sent the message
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }
    }

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View view;

        if (viewType == VIEW_TYPE_MESSAGE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.sender_message_layout,parent,false);
            return new SenderMessageHolder(view);

        } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.receiver_message_layout,false);
            return new ReceiverMessageHolder(view);
        }
    return null;
    }
             public void onBindViewHolder(RecyclerView.ViewHolder holder,int position) {
                LastMessageContent message = (LastMessageContent) msgDtoList.get(position);
            
                switch (holder.getItemViewType()) {
                    case VIEW_TYPE_MESSAGE_SENT:
                        populateSentViewHolder(holder,position);   break;
                    case VIEW_TYPE_MESSAGE_RECEIVED:
                        populateReceivedViewHolder(holder,position);
            
                }

 @SuppressLint("ResourceAsColor")
    private void populateSentViewHolder(RecyclerView.ViewHolder holder,int position) {
        MyMessageModal msgDto = this.msgDtoList.get(position);
        ((SenderMessageHolder) holder).rightMsgTextView.setText(msgDto.getmMSg());
}

 @SuppressLint("ResourceAsColor")
    private void populateReceivedViewHolder(RecyclerView.ViewHolder holder,int position) {
        MyMessageModal msgDto = this.msgDtoList.get(position);
        ((ReceiverMessageHolder) holder).leftMsgTextView.setText(msgDto.getmMSg());
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...