简体   繁体   English

RecyclerView notifyDataSetChanged()不起作用

[英]RecyclerView notifyDataSetChanged() not working

I'm using firebase for chat app and I'm implementing to fetch messages from firebase database and update recycler view. 我正在将Firebase用于聊天应用程序,并且正在实现从Firebase数据库中获取消息并更新回收者视图。

When I click the "send" button, it acts like below. 当我单击“发送”按钮时,它的行为如下。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final ChatModel.Comment comment = new ChatModel.Comment();
        comment.uid = uid;
        comment.message = editText.getText().toString();
        FirebaseDatabase.getInstance().getReference().child("chatrooms")
                .child(chatroomUid).push().setValue(comment).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                FirebaseDatabase.getInstance().getReference().child("chatrooms")
                        .child(chatroomUid).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        comments.clear();
                        for(DataSnapshot item : dataSnapshot.getChildren()) {
                            if(item.getKey().compareTo("users")!=0) {
                                comments.add(item.getValue(ChatModel.Comment.class));
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });
    }
});

Process is like this. 过程就是这样。

  1. Store message data into firebase database using "Comment" object form. 使用“评论”对象形式将消息数据存储到Firebase数据库中。
  2. If stage 1 is succeeded, fetch that message and call notifyDataSetChanged() 如果阶段1成功,则获取该消息并调用notifyDataSetChanged()

But it is not working, I have global adpater object and above method is called by this variable. 但它不起作用,我有全局adpater对象,并且上述方法由此变量调用。

Adapter code is like below. 适配器代码如下。

private class messageRecyclerveiwAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_message, viewGroup, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((MessageViewHolder)viewHolder).textView.setText(comments.get(i).message);
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }
}

How can I fix it? 我该如何解决?

You need to create an object of your Adapter in the same class as you have the setOnClickListener: 您需要在与setOnClickListener相同的类中创建Adapter的对象:

Something like this: 像这样:

final MessageRecyclerViewAdapter adapter = new MessageRecyclerViewAdapter(getActivity(), messageArrayList);

Then you need to pass the adapter to the method you're using to get the messages. 然后,您需要将适配器传递给用于获取消息的方法。 Right now, you're calling notifyDataSetChanged() in the adapter which is wrong. 现在,您正在错误的适配器中调用notifyDataSetChanged() First you need a specific method to get those messages from Firebase. 首先,您需要一种特定的方法来从Firebase获取这些消息。 For example: 例如:

public void getLiveChatMessages(final ArrayList<ChatMessageClass> messageArrayList, final MessageRecyclerViewAdapter adapter) {

I'm guessing you have a class that returns different part of the messages.. That's what I've named "ChatMessageClass" here, but you may call it something else. 我猜你有一个返回消息的不同部分的类。这就是我在这里命名为“ ChatMessageClass”的东西,但是你可以用别的名字来称呼它。 Within this method you get the messages from Firebase, then afterward you call as the last thing in the method: 在此方法中,您从Firebase获取消息,然后在该方法中最后调用:

adapter.notifyDataSetChanged();}

within the getLiveChatMessages method. 在getLiveChatMessages方法中。 Here you also pass in the adapter so that you can use it. 在这里,您还可以传入适配器,以便可以使用它。 This is the way to call the notifyDataSetChanged() within the same method as where you get the messages. 这是在与获取消息相同的方法中调用notifyDataSetChanged()的方法。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM