我正在从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 ();
}
}
}
这是Adapter类
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]: https://i.stack.imgur.com/dFD6z.jpg
答案 0 :(得分:3)
我不认为Firebase可以让您获取所有用户帐户信息,这是我在项目中所做的工作,在该项目中,我使用readtime DataBase来以这种格式签约保存用户信息(以后可以使用Uid更新信息) ,然后为recyclerView获取此数据库字段。
注意:应该使用用户身份验证UID来获取数据。
在这种情况下,也请针对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) { }
});
}
答案 1 :(得分:2)
对我来说,我会将所有子项存储到list
中,然后将列表传递到recyclerView
方法内的onDataChange
适配器中。
修改
从最新代码开始,您没有将返回对象添加到mcontacts
列表中。检查以下代码。
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()){
Contacts contacts =npsnapshot.getValue(Contacts.class);
mcontacts.add(contacts); // you miss this line
}
mAdapter = new UsersAdapter ( getApplicationContext () , mcontacts );
recyclerView.setAdapter ( mAdapter );
答案 2 :(得分:1)
我的建议是创建用户类
class User
{
//filds like name,image etc
}
现在您是recyclerView适配器类
class RcUsersAdapter extends RecyclerView.Adapter<RcUsersAdapter.ViewHolder>
{
public ArrayList<User> userList;
public RcUsersAdapter(Context context,ArrayList<Users> userList)
{
this.context = context;
this.userList = userList;
}
//... other methods
}
在您的Activity或Fragment类中知道如何使用
初始化适配器RcUsersAdapter rcUsersAdapter = new RcUsersAdapter(context,new ArrayList<User>());
知道何时获取数据
rcUsersAdapter.userList.add(UserClassObject);
rcUsersAdapter.notifyItemInserted(rcUsersAdapter.getItemCount());
答案 3 :(得分:0)
如何使用FirebaseRecyclerAdapter(FirebaseRecyclerOptions)。
答案 4 :(得分:0)
您的代码未完成@HemendraGangwar的注释。正如@JohnJoe所写,您需要在方法onDataChange()
中添加每个孩子。我建议您做这样的事情:
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exist) {
for (DataSnapshot ds : dataSnapshot.getChildren ()){
// Create your object wrapper with fields that you have in db.
MyUser user = ds.getValue(MyUser.class);
// Method in adapter that will add the user to list
// and run notifyItemInserted(insertedPosition);
adapter.addUser(user);
}
}
}
通过DP ViewHolder在适配器方法usersnames.setText()
中设置onBindViewHolder
,例如:holder.usersnames.setText(user.getName());
。