我在使用回收视图时遇到了问题,它仅显示了从Firebase提取的一位用户。
我的回收适配器如下:
FirebaseRecyclerAdapter<Users, FriendsViewHolder> adapter = new FirebaseRecyclerAdapter<Users, FriendsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final FriendsViewHolder holder, int position, @NonNull Users model)
{
String userIDs = getRef(position).getKey();
UsersRef.child(userIDs).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
String name = dataSnapshot.child("name").getValue().toString();
String number = dataSnapshot.child("phone").getValue().toString();
Log.d("USERNAME", name);
holder.username.setText(name);
holder.phone.setText(number);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@NonNull
@Override
public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.friend_list,viewGroup,false);
FriendsViewHolder viewHolder = new FriendsViewHolder(view);
return viewHolder;
}
};
当我调试程序时,log.d(“ USERNAME”,name);仅显示我的树中有1个节点。
当我循环FirebaseRecyclerAdapter时,它将显示我想要的所有节点:
FirebaseRecyclerAdapter<Users, FriendsViewHolder> adapter = new FirebaseRecyclerAdapter<Users, FriendsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final FriendsViewHolder holder, int position, @NonNull Users model)
{
for(int i=0;i<=position;i++)
{
String userIDs = getRef(i).getKey();
UsersRef.child(userIDs).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
String name = dataSnapshot.child("name").getValue().toString();
String number = dataSnapshot.child("phone").getValue().toString();
Log.d("USERNAME", name);
holder.username.setText(name);
holder.phone.setText(number);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@NonNull
@Override
public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.friend_list,viewGroup,false);
FriendsViewHolder viewHolder = new FriendsViewHolder(view);
return viewHolder;
}
};
该怎么办,这样我才能在我的回收站视图中显示所有节点