gpt4 book ai didi

java - 从 FirebaseDatabase 检索所有用户

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:02 27 4
gpt4 key购买 nike

我正在从 firebase 数据库中检索所有用户并将它们显示在 recyclerView 中。它显示城镇、用户名和图像。我能够检索,但它只显示一个用户而不是所有用户。数据库有很多用户,但只显示一个。任何建议都会有帮助。

这是检索用户的位

    public void showUsersList() {

mdatabaseRef = FirebaseDatabase.getInstance ().getReference ( "Users" );

mdatabaseRef.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

mcontacts.clear ();

for (DataSnapshot postSnapshot : dataSnapshot.getChildren ()) {

Contacts contactsz = dataSnapshot1.getValue (Contacts.class);
mcontacts.add ( contactsz );

}

mAdapter = new UsersAdapter ( getApplicationContext () , mcontacts );
recyclerView.setAdapter ( mAdapter );

}

@Override
public void onCancelled(DatabaseError databaseError) {

Toast.makeText ( FindFriendsActivity.this , " " , Toast.LENGTH_SHORT ).show ();

}
}



}

这是适配器类

     private Context mcontext;
private List<Contacts> mcontacts;

public UsersAdapter(Context context, List<Contacts> contacts){

mcontext = context;
mcontacts = contacts;

}


@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup , int i) {

View v = LayoutInflater.from ( mcontext ).inflate ( R.layout.users_display_layout, viewGroup, false );
return new UsersViewHolder ( v );

}

@Override
public void onBindViewHolder(@NonNull UsersViewHolder usersViewHolder , int i) {

Contacts contactsCurrent = mcontacts.get ( i );
usersViewHolder.nameOfUser.setText ( contactsCurrent.getName () );
usersViewHolder.nameOfTown.setText ( contactsCurrent.getTownname () );

Picasso.get ().load ( contactsCurrent.getImage () ).into ( usersViewHolder.usersImage );

}

@Override
public int getItemCount() {
return mcontacts.size ();
}

public class UsersViewHolder extends RecyclerView.ViewHolder{

public TextView nameOfUser;
public TextView nameOfTown;
public CircleImageView usersImage;

public UsersViewHolder(@NonNull View itemView) {
super ( itemView );

nameOfUser = itemView.findViewById ( R.id.user_profile_name );
nameOfTown = itemView.findViewById ( R.id.user_town_name );
usersImage = itemView.findViewById ( R.id.users_profile_image );


}
}

}
`````````````````````````````````````````````````````````````````

这是联系人类

public class Contacts {

public String Name,townname,image;


public Contacts(){

}


public Contacts(String name , String townname , String image) {
Name = name;
this.townname = townname;
this.image = image;

}


public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getTownname() {
return townname;
}

public void setTownname(String townname) {
this.townname = townname;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}
}


[1]: /image/dFD6z.jpg

最佳答案

我不认为 Firebase 可以让你获取所有用户帐户信息,这是我在一个项目中工作时所做的,当他们以这种格式注册时,我使用 readtime DataBase 来保存用户信息(稍后可以使用 Uid 更新信息) ,然后为 recyclerView 获取此数据库字段。

注意:应该使用用户身份验证 UID 来获取数据。

enter image description here

对于这种情况,也可以为 recyclerView 尝试使用此方法,而不是 onDataChange。

mFirebaseDatabaseRef.child("Users").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

String retrievename = String.valueOf((String) dataSnapshot.child("Name").getValue());
String retrievecity = String.valueOf((String) dataSnapshot.child("townname").getValue());
String retrieveProfileImage = String.valueOf((String) dataSnapshot.child("image").getValue());

if (!retrievename.equals("null")) {

usersnames.setText ( retrievename );
userstown.setText ( retrievecity );
Picasso.get().load ( retrieveProfileImage ).into ( circleImageView );
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) { }
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
@Override
public void onCancelled(DatabaseError databaseError) { }
});
}

关于java - 从 FirebaseDatabase 检索所有用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55567799/

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