我正在从Firebase数据库中读取用户(姓名,年龄(的数据,并将其显示在回收器视图中。当用户长按某个项目时,该项目会成功从 firebase 中删除,但是当我尝试从用户列表中删除该项目时,例如列表,在子侦听器的 onChildRemove 方法中,用户不会被删除。代码为:
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;}
}
回收器视图代码如下:
@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;
}
});
}
我还尝试将返回的用户传递给适配器,但这不起作用。
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(如何从回收商视图列表中删除长时间单击项目时删除的项目?
2(我在MainActivity.java
有List<User> mDataList
,在UserAdapter.java
有一个。 我正在将mDataList
MainActivity
传递给UserAdapter
. 这两者在内存中是相同还是不同?如果不同,那么更改MainActivity
中的List<user> mDataList
(在onChildAdded
中添加新项目(如何更改UserAdapter
中的List<User> mDataList
? 多谢。 我已经尽力澄清我的问题。
使用 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;
}
}
现在更新您的 onChildRemove 方法:
@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。