java - 单击recyclerview上的项目时的多个数据
问题描述
我尝试显示聊天列表,但是当我单击项目时,recyclerview 中的列表是重复的,如图 1 所示,但是当我更改片段(底部导航)时,列表是正常的(不重复)。我的代码有什么问题,请帮忙
这里的代码 AdapterChatlist
public class AdapterChatlist extends RecyclerView.Adapter<AdapterChatlist.Holder>{
Context context;
List<User> userList;//get user info
private HashMap<String, String> lastMsgMap;
public AdapterChatlist(Context context, List<User> userList) {
this.context = context;
this.userList = userList;
lastMsgMap = new HashMap<>();
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//inflate layout row_chatlist
View view = LayoutInflater.from(context).inflate(R.layout.row_chatlist, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
String hisUid = userList.get(position).getId();
String userImg = userList.get(position).getImageUrl();
String userName = userList.get(position).getFullname();
String lastMsg = lastMsgMap.get(hisUid);
holder.nameTv.setText(userName);
if (lastMsg==null || lastMsg.equals("default")){
holder.lastMsgTv.setVisibility(View.GONE);
}else {
holder.lastMsgTv.setVisibility(View.VISIBLE);
holder.lastMsgTv.setText(lastMsg);
}
try {
Glide.with(context).load(userImg)
.placeholder(R.drawable.ic_pic)
.into(holder.profileIv);
}catch (Exception e){
Glide.with(context).load(R.drawable.ic_pic)
.into(holder.profileIv);
}
if (userList.get(position).getOnlineStat().equals("online")){
holder.onlineStatIv.setImageResource(R.drawable.ic_online);
}else {
holder.onlineStatIv.setImageResource(R.drawable.ic_offline);
}
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userid", hisUid);
context.startActivity(intent);
});
}
public void setLastMsgMap(String userId, String lastMsg){
lastMsgMap.put(userId, lastMsg);
}
@Override
public int getItemCount() {
return userList.size();
}
class Holder extends RecyclerView.ViewHolder{
ImageView profileIv, onlineStatIv;
TextView nameTv, lastMsgTv;
public Holder(@NonNull View itemView) {
super(itemView);
profileIv = itemView.findViewById(R.id.profileIv);
onlineStatIv = itemView.findViewById(R.id.onlineStatIv);
nameTv = itemView.findViewById(R.id.nameTv);
lastMsgTv = itemView.findViewById(R.id.lastMsgTv);
}
}}
这里是ChatlistFragement的代码
private void loadChats() {
usersList = new ArrayList<>();
ref = FirebaseDatabase.getInstance().getReference("Users");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
User users = ds.getValue(User.class);
for (Chatlist chatlist : chatlistList) {
if (users.getId() != null && users.getId().equals(chatlist.getId())) {
usersList.add(users);
break;
}
}
adapterChatlist = new AdapterChatlist(getContext(), usersList);
//setAdapter
rv_chatsList.setAdapter(adapterChatlist);
//set last msg
for (int i = 0; i < usersList.size(); i++) {
lastMsg(usersList.get(i).getId());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void lastMsg(final String userId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String theLastMsg = "default";
for (DataSnapshot ds : snapshot.getChildren()) {
Chat chat = ds.getValue(Chat.class);
if (chat == null) {
continue;
}
String sender = chat.getSender();
String receiver = chat.getReceiver();
if (sender == null || receiver == null) {
continue;
}
if (chat.getReceiver().equals(currentUser.getUid()) && chat.getSender().equals(userId) ||
chat.getReceiver().equals(userId) && chat.getSender().equals(currentUser.getUid())) {
theLastMsg = chat.getMessage();
}
}
adapterChatlist.setLastMsgMap(userId, theLastMsg);
adapterChatlist.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
解决方案
使用“adapterChatlist.setLastMsgMap(userId, theLastMsg);” 您正在将一些新用户添加到现有列表中。我的猜测是,您应该有一个包含所有要显示的项目的列表,并始终清除适配器拥有的旧列表。因为这个 onDataChange() 可能不止一次被调用多次,这就是你得到更多项目的原因。
推荐阅读
- android - 更改应用程序中的内容
- css - 禁用时如何更改Angular 5材质中的样式 - 选择选定文本
- css - 在 Safari (iOS) 上对图片进行缩放转换
- java - 无法从 Java 连接到 MySQL:MySQL 驱动程序连接逻辑中的 NullPointerException
- python - Tensorflow:如何转换 tensorflow LSTM 的输入数据?
- .net-core - 为什么我的验证器无法捕获必填字段错误
- angular - 结构类型和多态 - 添加超类型
- python - python 无法让代码正常工作
- laravel - Laravel 计数 > N
- vagrant - Vagrant Up (private_network) 错误:默认值:警告:连接中止。重试