gpt4 book ai didi

java - RecyclerView 的 onBindview Holder 不适用于第二个位置,但我在 getItemCount 中计数为 3

转载 作者:行者123 更新时间:2023-12-02 01:53:39 58 4
gpt4 key购买 nike

我从 firebase 获取数据,并将其填充到 userMessageList.adapter 中,并收到通知,并且 getItemCount 显示正确的计数,但 onBindViewHolder 仅运行一次对于用户消息列表的第 1 个位置。 onBindViewHolder 不适用于进一步的位置或数据

我没有找到与我的问题相关的解决方案

THIS IS MY ADAPTER CLASS

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {

private List <Messages> userMessagesList;
private DatabaseReference firebaseDatabase,users;
private FirebaseAuth auth;

public MessageAdapter(List<Messages> userMessagesList){

this.userMessagesList = userMessagesList;
}


@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_messages_layout,viewGroup,false);

auth = FirebaseAuth.getInstance();

return new MessageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final MessageViewHolder messageViewHolder, int i) {



String messageSenderID = auth.getCurrentUser().getUid();
Messages messages = userMessagesList.get(i);

String fromUserID = messages.getFrom();
String fromMessageType = messages.getType();

users = FirebaseDatabase.getInstance().getReference().child("Users").child(fromUserID);

users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

if(dataSnapshot.hasChild("image")){

final String receiverImage = dataSnapshot.child("image").getValue().toString();

Picasso.get()
.load(receiverImage)
.placeholder(R.drawable.profile_image)
.into(messageViewHolder.receiverProfileImage);


}
}

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

}
});

if(fromMessageType.equals("text")){

messageViewHolder.receiverMessageText.setVisibility(View.INVISIBLE);
messageViewHolder.receiverProfileImage.setVisibility(View.INVISIBLE);

if(fromUserID.equals(messageSenderID)){

messageViewHolder.senderMessageText.setBackgroundResource(R.drawable.sender_messages_layout);
messageViewHolder.senderMessageText.setTextColor(Color.BLACK);
messageViewHolder.senderMessageText.setText(messages.getMessage());
}
else{

messageViewHolder.receiverMessageText.setVisibility(View.INVISIBLE);
messageViewHolder.receiverProfileImage.setVisibility(View.VISIBLE);
messageViewHolder.receiverMessageText.setVisibility(View.VISIBLE);

messageViewHolder.receiverMessageText.setBackgroundResource(R.drawable.receiver_messeges_layout);
messageViewHolder.receiverMessageText.setTextColor(Color.BLACK);
messageViewHolder.receiverMessageText.setText(messages.getMessage());


}
}

}

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


public class MessageViewHolder extends RecyclerView.ViewHolder{

public TextView senderMessageText, receiverMessageText;

public CircleImageView receiverProfileImage;


public MessageViewHolder(@NonNull View itemView) {
super(itemView);

senderMessageText =itemView.findViewById(R.id.sender_message_text);
receiverMessageText = itemView.findViewById(R.id.receiver_message_text);
receiverProfileImage =itemView.findViewById(R.id.message_profile_image);

}
}
}

THIS IS ACTIVITY CODE WHERE IAM POPULATING THE userMessageList

rootRef.child("Messages")
.child(messageSenderID).
child(messageReceivedID)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot
dataSnapshot, @Nullable String s) {

Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();

}

}

最佳答案

正如上面评论中的 Pemba Tamang 所说,您的适配器中不应该有任何网络或数据库相关的代码。

解决您问题的算法:1. 获取适配器外部的消息列表。2. 订阅适配器外部的子更改。3. 当子项更改时,转到查看模型,通过其 id 找到此消息并更新此信息。4.更新您的适配器

如果您使用 DiffUtils 更新您的适配器,也会很好。它只会绑定(bind)更改的子项,而不是全部。请参阅:https://developer.android.com/reference/android/support/v7/util/DiffUtil

关于java - RecyclerView 的 onBindview Holder 不适用于第二个位置,但我在 getItemCount 中计数为 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411344/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com