简体   繁体   中英

How to get the list of users from Firebase RealTime database?

I need some help with Firebase Realtime Database request.

I have the following list of users that looks like this: 在此处输入图像描述

I want to implement something like a search screen that will show all users, but I have a problem with retrieving data from firebase.

Here is the method that sending requests to Firebase.

public MutableLiveData<List<SearchedUser>> getSearchedUsersMutableLiveData() {

        user.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    mSearchedUser.add(dataSnapshot.getValue(SearchedUser.class));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.d("myDebug", "onCancelled: ");
            }
        });
        mSearchedUsersMutableLiveData.setValue(mSearchedUser);
        return mSearchedUsersMutableLiveData;
    }

And here is the model class:

public class SearchedUser {

    private String mUserName;
    private String mUserEmail;
    private String mUserUid;

    public SearchedUser() {
    }

    public String getUserName() {
        return mUserName;
    }

    public void setUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public String getUserEmail() {
        return mUserEmail;
    }

    public void setUserEmail(String mUserEmail) {
        this.mUserEmail = mUserEmail;
    }

    public String getUserUid() {
        return mUserUid;
    }

    public void setUserUid(String mUserUid) {
        this.mUserUid = mUserUid;
    }
}

When I'm debugging the method dataSnapshot.getValue(SearchedUser.class) , all values in this class is null.

But when I'm debugging method dataSnapshot.getChildren() , I receive DataSnapshot { key = I7XA6J7uNseaPfWQxcraiTi3CZq1, value = {UserDetails={userEmail=admin@eag.com, userName=Misha}} } .

And dataSnapshot.getChildrenCount() retrieving me the count of users.

Someone can explain me what should I do?

There are two main problems in your code.

  1. When you are using the user reference which is defined as:

     private DatabaseReference user = db.getReference("Users");

And attach a listener on it, when looping through the snapshot object using getChildren() method, you aren't getting SearchedUser objects as you expected and this is because the direct child of the dataSnapshot is a child of type String named UserDetails and not a SearchedUser object. To be able to get those SearchedUser objects, a call to .child("UserDetails") is needed, as shown in the following lines of code:

user.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()){
            mSearchedUser.add(dataSnapshot.child("UserDetails").getValue(SearchedUser.class));
//                                        ^^^^^^^^^^^^^^^^^^^^^
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.d("myDebug", "onCancelled: ");
    }
});
  1. To be able to get those SearchedUser objects, the name of the properties in your class should match the name of the properties in the database. See, you have a field named mUserEmail in your SearchedUser class, while in the database is called userEmail and this is not correct. To solve this, you can simply change the name of all properties in your class to match the one in the database.

Also don't forget to move the following line of code:

mSearchedUsersMutableLiveData.setValue(mSearchedUser);

Right after the for loops ends:

for (DataSnapshot dataSnapshot : snapshot.getChildren()){
    mSearchedUser.add(dataSnapshot.getValue(SearchedUser.class));
}
mSearchedUsersMutableLiveData.setValue(mSearchedUser);

Hy michael, all the method of firebase is asynchronous so while in this method

public MutableLiveData<List<SearchedUser>> getSearchedUsersMutableLiveData() {

        user.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    mSearchedUser.add(dataSnapshot.getValue(SearchedUser.class));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.d("myDebug", "onCancelled: ");
            }
        });
        mSearchedUsersMutableLiveData.setValue(mSearchedUser);
        return mSearchedUsersMutableLiveData;
    }

you're return value first while your firebase method is still fetching value from database as a result it'll always null.

As per my opinion you should set your adapter inside "onDataChange" method outside for loop and would always fetch data and fill your list as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM