0

I am new to android and just learned to create a users profile like facebook or instagram, currently I have registered four users with email/password and in database am storing there email, name, phone. In another activity am trying to fetch only the names of the registered users like "People you may know" in facebook, but it is displaying nothing.

private void showData(DataSnapshot dataSnapshot) {

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

        UserInformation userInformation = new UserInformation();

        userInformation.setName(ds.child("users").child("name").getValue(UserInformation.class).getName());

        ArrayList<String> array = new ArrayList<>();

        array.add(userInformation.getName());

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array);

        mListView.setAdapter(adapter);

    }

is this correct way of fetching it because am getting NullPointerException, UserInformation is model class containing getters and setters.

fire-18b42
           users
                KWE45gijkfJDK6782IBkjas(UID)
                    email: "@example.com"
                    name: "Example"
                    phone_num: 10000000

This is how I have stored the data to database once the user is registered, now like this I have four users and I want to extract every ones name in seperate listView.

2 Answers2

3

To display those names, please use this code:

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);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersdRef.addListenerForSingleValueEvent(eventListener);

And the output will be in this case all your user names.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Its working good with logs but its not displaying all the names in the list it just displays one name – Kaushal Topinkatti Jul 13 '17 at 14:29
  • Probably it displays the last name. This is happening because the declaration of the `ArrayList` must be inside the `onDataChange()` method and outside the `for loop`. Beeing inside the `for loop` means that you are creating a new instante of the `ArrayList` every time you loop. – Alex Mamo Jul 13 '17 at 14:53
  • @AlexMamo Good afternoon Alex, I have one question? What if I have a list view and an adapter, how can I insert them in the code to display it respectively? Thank you – Tomas Mota Mar 22 '20 at 18:49
  • @TomasMota You can check **[this](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** out. – Alex Mamo Mar 22 '20 at 18:59
0

Firebase is NoSql. You need to retrieve all users from myRef.child("users") and apply iterator to get names of all users.

Usman Rana
  • 2,067
  • 1
  • 21
  • 32