0

In this example I have a Firebase database, so I can get the elements and keys from the database for that. I have used some libraries and functions to handle the database. The problem was after I got the data and put it down in ArrayList named comments_keys, I used a loop to print items inside it, then I printed: I/Get_KEY:: comments four consecutive times.

Now why doesn't it print Count equals 4?

I want to get a value in the number of items on the inside ArrayList. How do I do that?

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot child: dataSnapshot.getChildren()) {
        HashMap < String,
        HashMap < String,
        HashMap < String,
        String >>> values = (HashMap < String, HashMap < String, HashMap < String, String >>> ) child.getValue();

        ArrayList < String > comments_keys = new ArrayList < String > (values.keySet());
        int Count = 0;

        for (String k: comments_keys) {
            players.put(k, String.valueOf(values.get(k)));
            Log.i("GetValues :", players.values().toString());
            Log.i("Get_KEY: ", k);
            Count += 1;
        }
        Log.i("GetCount) : ", String.valueOf(Count));
    }
}
// Log Cat Get_KEY: 
05-17 11:09:37.455 25024-25024/com.ahmedcomm.firebaseapp I/Get_KEY:: comments
05-17 11:09:37.455 25024-25024/com.ahmedcomm.firebaseapp I/Get_KEY:: comments
05-17 11:09:37.455 25024-25024/com.ahmedcomm.firebaseapp I/Get_KEY:: comments
05-17 11:09:37.455 25024-25024/com.ahmedcomm.firebaseapp I/Get_KEY:: comments

// Log Cat Get_KEY: 
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetValues :: [{-LeT9FJNoQaGIz04t5Ux={status=1, rate=4, comment=yes}}]
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetValues :: [{-LeTA3vsxwsKuhvcITVE={status=0, rate=2, comment=i don't like it}, -LeWdgceDbsf4eKkQW6B={status=0, rate=3, comment=okys}}]
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetValues :: [{-LeTAVMVV2aZcpZ3UelP={status=1, rate=5, comment=i like it}}]
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetValues :: [{-LeTBX4SIWPAoYzCdgCq={status=1, rate=5, comment=ملعب ممتاز}, -LeTCUtavtH3HzNc_CBY={status=0, rate=3, comment=not pad}, -LeTCDwJ9Ptc-DC9RMxE={status=1, rate=5, comment=good}}]

// Log Cat GetCount:
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetCount) :: 1
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetCount) :: 1
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetCount) :: 1
05-17 11:23:38.005 25410-25410/com.ahmedcomm.firebaseapp I/GetCount) :: 1
5
  • 1
    comments_keys.size() ? Commented May 17, 2019 at 9:36
  • I used comments_keys.size() , but it printed : 05-17 11:47:07.770 27078-27078/com.ahmedcomm.firebaseapp I/GetCount) :: 1 05-17 11:47:07.770 27078-27078/com.ahmedcomm.firebaseapp I/GetCount) :: 1 05-17 11:47:07.770 27078-27078/com.ahmedcomm.firebaseapp I/GetCount) :: 1 05-17 11:47:07.770 27078-27078/com.ahmedcomm.firebaseapp I/GetCount) :: 1 Commented May 17, 2019 at 9:48
  • 1
    Maybe it's printed four times because your methos is called 4 times. But your ArrayList has a sze of 1. Commented May 17, 2019 at 9:56
  • What do I do to find the number of items ? Commented May 17, 2019 at 10:40
  • The number of item is 1 Commented May 17, 2019 at 13:20

1 Answer 1

2

I don't know why you're complicating your code. What does this weird casting do?

(HashMap < String, HashMap < String, HashMap < String, String >>> ) child.getValue();

To simplify everything, you can do this:

class MyClass {
   // This is your model that matches the data in Firebase
   // put here the properties as in DB
   // for example
   String name;
   // and so on.
}

List<MyClass> myClassList = new ArrayList<>();

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
 MyClass myClass = snapshot.getValue(MyClass.class);
 myClassList.add(myClass);
}

int size = list.size(); // this is the count
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.