Users list empty

2019-09-20 06:48发布

问题:

I'm trying to retrieve from RealTime-Database the list of users with their First name and second name but nothing appears.

The class UsersActivity show all the users from the database.I tried a lot of tutorials but nothing seems to work.

public class UsersActivity extends AppCompatActivity {
    private RecyclerView userslist;
    private DatabaseReference ref;
    private List<Users> mUsers;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_users );
        ref = FirebaseDatabase.getInstance ().getReference ().child ( "FirstName" );

        userslist = (RecyclerView) findViewById ( R.id.users_list );

        userslist.setHasFixedSize ( true );
        userslist.setLayoutManager ( new LinearLayoutManager ( this ) );


    }
    public void readUsers(){
        FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
        DatabaseReference reference=FirebaseDatabase.getInstance ().getReference ("spors-budy-en866");
        reference.addValueEventListener ( new ValueEventListener () {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onCancelled (@NonNull DatabaseError databaseError) {

            }
        } )
    }

    @Override
    protected void onStart () {
        super.onStart ();
        FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users> ()
                .setQuery ( ref, Users.class )
                .build ();
        FirebaseRecyclerAdapter<Users, FindFriendViewHolder> adapter = new FirebaseRecyclerAdapter<Users, FindFriendViewHolder> ( options ) {
            @Override
            protected void onBindViewHolder (@NonNull FindFriendViewHolder findFriendViewHolder, int i, @NonNull Users users) {
                findFriendViewHolder.userName.setText ( users.getName () );

            }

            @NonNull
            @Override
            public FindFriendViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.users_template, parent, false );
                FindFriendViewHolder viewHolder = new FindFriendViewHolder ( view );
                return viewHolder;
            }
        };
        userslist.setAdapter ( adapter );
        adapter.startListening ();
    }

    public static class FindFriendViewHolder extends RecyclerView.ViewHolder {
        TextView userName;
        ImageView profileImage;

        public FindFriendViewHolder (@NonNull View itemView) {
            super ( itemView );
            userName = itemView.findViewById ( R.id.username1 );
            profileImage = itemView.findViewById ( R.id.profilepicture );

        }


    }
}

The rules of my database:

{
  "rules": {
    ".read": "true",
      ".write":"true",
    "users":{
    "$user_id":{
       ".read": "auth.uid === $user_id",
       ".write": "auth.uid === $user_id"

    }
  }
}
}

I expect to show all the users from my databse.But the activity is empty.

回答1:

You're setting :

ref = FirebaseDatabase.getInstance().getReference().child("FirstName");

And then use that to initialize the FirebaseUI adapter:

FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users> ()
        .setQuery(ref, Users.class )

While I don't know your data model, it seems unlikely that you'd have a list of users under /FirstName, which is what is required to show those users with the FirebaseUI adapter.

More likely ref should be initialized as:

ref = FirebaseDatabase.getInstance().getReference("users")


回答2:

1-   ref=firebaseDatabase.getReference("FirstName");
2-   ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  //**HERE WHERE YOU SHOULD FETCH THE DATA FROM YOUR DATABASE**



           String value=dataSnapshot.getValue(String.class);

            list.add(value);

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                 list.clear();
                String value=dataSnapshot.getValue(String.class);

                list.add(value);
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

and finally your database should be like this : hope that works for you