gpt4 book ai didi

java - List 不删除它包含在 RecyclerView 适配器中的 POJO 对象

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:16 25 4
gpt4 key购买 nike

我正在从 Firebase 数据库中读取用户(姓名、年龄)的数据并将其显示在回收站 View 中。当用户长按一个项目时,该项目已成功从 firebase 中删除,但是当我尝试从 List of users 中删除该项目时,例如 List ,在子监听器的 onChildRemoved 方法中,用户未被删除。代码是:

mChildListener=new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

User user=dataSnapshot.getValue(User.class);
user.setUid(dataSnapshot.getKey());

mDataList.add(user);
mUserAdapter.notifyDataSetChanged();
//passing the user returned to adapter but didn't work
mUserAdapter.updateDataSet(user);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
User user=dataSnapshot.getValue(User.class);
user.setUid(dataSnapshot.getKey());

mDataList.remove(user);
mUserAdapter.notifyDataSetChanged();
}
};

这是用户模型类:

public class User {
private String uid;
private String name;
private int age;
public User() {}
public User(String uid, String name, int age) {
this.uid = uid;
this.name = name;
this.age = age;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getUid() {return uid;}
public void setUid(String uid) {this.uid = uid;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
}

回收器 View 代码是这样的:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

User user=mDataList.get(position);

holder.textView.setText(user.getName()+ " | "+user.getAge());

holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uid=user.getUid();
Intent intent=new Intent(mContext,DetailsActivity.class);
intent.putExtra(USER_KEY,uid);
mContext.startActivity(intent);
}
});
holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//this line returns true
boolean remove = mDataList.contains(user);
//if I try to remove the user here, its removed
//this line works as I want
// mDataList.remove(user);
Log.d(MainActivity.TAG, "updateDataSet: Contains: "+remove);
Task<Void> removeUser = NetworkUtils.removeUser(user.getUid());
removeUser.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(mContext, "User removed", Toast.LENGTH_SHORT).show();
//mDataList.remove(user);
//notifyDataSetChanged();
}
});
return true;
}
});
}

我还尝试将 onChildRemoved 中返回的用户传递给适配器,但效果不佳。

public void updateDataSet(User user){
//this also returns false even the data of this user id displayed
boolean contains=mDataList.contains(user);
//this also does not works
mDataList.remove(user);
this.notifyDataSetChanged();
}

我的问题是:

1) 我怎样才能从回收者 View 列表中删除在长时间点击项目时删除的项目?

2) 我有 List<User> mDataListMainActivity.java和一个 UserAdapter.java .我正在传递 mDataListMainActivityUserAdapter .这两个在内存中是相同的还是不同的?如果不同,那么如何改变 List<user> mDataListMainActivity (在 onChildAdded 中添加新项目)更改 List<User> mDataListUserAdapter ?非常感谢。我已尽力解决我的问题。

最佳答案

使用 equals() 方法更新您的 User 类:

public class User {
private String uid;
private String name;
private int age;
public User() {}
public User(String uid, String name, int age) {
this.uid = uid;
this.name = name;
this.age = age;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getUid() {return uid;}
public void setUid(String uid) {this.uid = uid;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}

@Override
public boolean equals(Object obj) {
if (obj instanceof User) {
User user= (User) obj;
return uid.equals(user.getUid());
} else return false;
}
}

现在更新您的 onChildRemoved 方法:

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
User user=dataSnapshot.getValue(User.class);
user.setUid(dataSnapshot.getKey());
int index = mDataList.indexOf(user);
mDataList.remove(index);
mUserAdapter.notifyDataSetChanged();
}

注意:如果uid在mDataList中不存在,则返回-1。

关于java - List<User> 不删除它包含在 RecyclerView 适配器中的 POJO 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52819309/

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