Search code examples
androidlistviewfirebasefirebase-realtime-databasedata-retrieval

How to get all users data from firebase database in custom listview in android


I have Firebase database with childs with uid of each user and this childs contain sub-childs like username, city, etc.

I want to get all the users username and city from the Firebase database.

How can I do this?

I tried many solutions from StackOverflow and other websites but I couldn't understand it.

So, please if you are replying me then mention all the things meaning properly in the answer.


Solution

  • Your answer is here, it worked for me it may work for you as well

    How to list the registered users names in Android (Firebase)?

    Here is my code which takes out the usernames of all users registered to my app just like the people you may know stuff of facebook

    //to fetch all the users of firebase Auth app
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    
        DatabaseReference usersdRef = rootRef.child("users");
    
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
    
                    String name = ds.child("name").getValue(String.class);
    
                            Log.d("TAG", name);
    
                            array.add(name);
    
                }
                ArrayAdapter<String> adapter = new ArrayAdapter(OtherUsersActivity.this, android.R.layout.simple_list_item_1, array);
    
                mListView.setAdapter(adapter);
    
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        };
        usersdRef.addListenerForSingleValueEvent(eventListener);
    }
    

    Hope this make work for you. Declare the ArrayList globally.