-2

My Child List Screenshot

My Child List Screenshot

I need to get the number of childs (No nested childs are there!) and store it in a global public variable. Also, I want all the childs to put in a string array list.

Thanks in advance!

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • 1
    Hi Welcome to stackoverflow. So what have you tried so far to achieve the same. – Santhosh Joseph Oct 16 '18 at 12:30
  • Hi santhosh, i have tried making a list variable outside the listener and and added every key value in for() loop, but didn't work, and for count i used the arraylist.size(); – Hafeez ul haq Oct 16 '18 at 13:13
  • @Hafeezulhaq Please have a look at [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). This is the best way for us to see what you're struggling with. – Frank van Puffelen Oct 16 '18 at 13:23

2 Answers2

1

Use some form of listener. For single lookup use the valueEventListener.

Then in the OnDataChange() method that is automatically generated. Use the datasnapshot to get the childrenCount and then to loo through the children and add them to the list

List<String> list = new ArrayList<>();
long childrenCount;
public void getListItems()
{
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            childrenCount = dataSnapshot.child("searchingUsers").getChildrenCount();
            for (DataSnapshot snap : dataSnapshot.child("searchingUsers").getChildren())
            {
                //If you want the node value
                list.add(snap.getValue().toString());

                //If you want the key value
                list.add(snap.getKey());
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException(); // don't ignore errors
        }
    });
}

This makes both the variables global in the Activity. So you can reach them from anywhere in the Activity

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Janwilx72
  • 502
  • 4
  • 18
  • Thank you for answer, but can you please give any idea how can use 'list' and childrenCount variable outside the Scope of Listener – Hafeez ul haq Oct 16 '18 at 13:10
  • You can't really. You'll have to move the code that needs the data **into** `onDataChange` or invoke it from there. See https://stackoverflow.com/questions/41163512/firebase-android-count-children-and-store-it-in-a-variable and https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list – Frank van Puffelen Oct 16 '18 at 13:22
  • See in the edit. When you declare the variables outside of the function you can reach them from anywhere in the activity, not just in the listener. I'm just not sure if this is best practice – Janwilx72 Oct 16 '18 at 13:25
  • The 'list' is still empty i don't know why though i tried public List itemsList() and return the list variable too, but didn't work! – Hafeez ul haq Oct 17 '18 at 05:27
  • Thanks for you contribution, got the solutions! by doing the task inside the scope of listener! – Hafeez ul haq Oct 17 '18 at 05:57
0

To get a list of all those random values, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference searchingUsersRef = rootRef.child("searchingUsers");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String value = ds.getValue(String.class);
            list.add(value);
        }

        //Do what you need to do with your list
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
searchingUsersRef.addListenerForSingleValueEvent(valueEventListener);

As you can see I have used the list only inside the callback, if you want to use the list outside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193