我正在尝试从“回收者”视图中的用户列表中过滤我的用户。但是我需要为onBindViewHolder中的每个用户获取uid,但我不知道如何。我也想将onBindViewHolder中的代码移动到ViewHolder的bind方法,但是我不知道如何获取上下文。
private void setRecyclerView() {
mUsersRecyclerView.setHasFixedSize(true);
mUsersRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new UserAdapter();
mUsersRecyclerView.setAdapter(mAdapter);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> list = dataSnapshot.getChildren();
// Filter current user
//List<User> userList = new ArrayList<>();
for (DataSnapshot user : list) {
if (!user.getKey().equals(mCurrentUid)) {
mUserList.add(user.getValue(User.class));
}
}
// Setting data
// mAdapter.setItems(userList);
}
@Override public void onCancelled(@NonNull DatabaseError databaseError) {}
});
}
private class UserAdapter extends RecyclerView.Adapter<UserViewHolder> {
@NonNull
@Override
public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_item_layout, parent, false));
}
@Override
public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
User user = mUserList.get(position);
holder.bind(user);
// TODO: move this to holder.bind() method
holder.setThumb_image(user.getThumb_image(),getApplicationContext());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent9 = new Intent(UsersActivity.this,ProfileActivity.class);
intent9.putExtra("user_id", uid); // TODO: get uid for each user here
startActivity(intent9);
}
});
}
@Override
public int getItemCount() {
return mUserList.size();
}
}
private class UserViewHolder extends RecyclerView.ViewHolder {
View mView;
public UserViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void bind(User user) {
TextView nameView = mView.findViewById(R.id.item_name);
nameView.setText(user.getName());
TextView statusView = mView.findViewById(R.id.item_status);
statusView.setText(user.getStatus());
}
public void setThumb_image(String thumb_image, Context ctx) {
CircleImageView imageView = mView.findViewById(R.id.item_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.mipmap.icon).into(imageView);
}
}
Here is a working code和FirebaseRecyclerAdapter不能过滤用户。
答案 0 :(得分:1)
在将数据存储到Firebase数据库中的同时在模型中添加firbaseId
这里是示例用户模型类,它将用户的信息存储到Firebase
public class User {
private String name;
private String address;
private String firebaseId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFirebaseId() {
return firebaseId;
}
public void setFirebaseId(String firebaseId) {
this.firebaseId = firebaseId;
}
}
然后像下面那样存储值
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference(DATABASE_NAME);
String fireBaseId = databaseReference.push().getKey();
if (fireBaseId != null) {
User user = new User();
user.setName("SSB");
user.setAddress("Some address");
user.setFirebaseId(fireBaseId);
databaseReference.child(fireBaseId).setValue(user);
}
然后您可以从用户模型类中获取ID