trango firmino
trango firmino

Reputation: 17

Cannot get count elements inside array list

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

Upvotes: 0

Views: 88

Answers (1)

Shaban Kamel
Shaban Kamel

Reputation: 21

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

Upvotes: 2

Related Questions