ImArtVandalay
ImArtVandalay

Reputation: 316

Android Firebase - Count the total number of occurrences of a value within a list

Within my project, users can create a list and populate the list with items. They can then set a single category for each list item. There are three categories (Yes, No, and Unknown).

I'm trying to get the count total for each category. Eg., A user's list contains 20 items --> 10 of the 20 are categorized as "Yes", 5 are "No", and the last 5 are "Unknown."

Here's where I'm at:

ListDetailsActivity:

listItemsReference = FirebaseDatabase.getInstance().getReference()
        .child("list-items")
        .child(listId);

    ...

listItemsReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()){
            Legislator legislator = child.getValue(Legislator.class);
            if (legislator.getCategory() != null){
                Log.d("category", legislator.getCategory());
            }

        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});

The Log returns the following:

09-23 00:23:36.995 7359-7359/com.example.me.firebasesimplelist2 D/category: Yes
09-23 00:23:36.995 7359-7359/com.example.me.firebasesimplelist2 D/category: Yes
09-23 00:23:36.995 7359-7359/com.example.me.firebasesimplelist2 D/category: No

How would I go about getting the totals for each category? Where, "Yes" would return 2, and "No" would return 1?

Here's part of the JSON if it helps:

{
  "-KSKFE4MIqJOVwHI7EXW" : {
    "smithJ" : {
      "category" : "Yes",
      "categorySet" : true,
      "name" : "John Smith",
    },
    "bobB" : {
      "category" : "Yes",
      "categorySet" : true,
      "name" : "Billy Bob",
    },
    "jonesS" : {
      "category" : "No",
      "categorySet" : true,
      "name" : "Susy Jones",
    }
  }
}

Am I making this more complicated than it needs to be? I'm getting the feeling that I'm overthinking it...

Upvotes: 2

Views: 2177

Answers (1)

Ali Bdeir
Ali Bdeir

Reputation: 4365

First off go the parent of your list of objects (above "-KSKFE4MIqJOVwHI7EXW" in your case) by going to your onCreate method or onCreateView (if you're using a fragment) and adding:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(yourParent);

And, add an int for each category:

int yes;
int no;
int unknown;

Now, add a ValueEventListener:

ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

And inside onDataChange, you need a foreach loop on each of the dataSnapshot's children:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
   for(DataSnapshot snapShot:dataSnapshot.getChildren(){
       // Go to the next step
     }
}

Now, see if it is "Yes", "No", or "Unknown" by adding this to the foreach loop we made:

switch(snapShot.child("category").getValue(String.class)){ //This statement is seeing what "category" is.
  case "Yes":
    ++yes; //This is the "yes" int we made earlier; ++ increments 1.
    break;
  case "No":
    ++no; //This is the "no" int we made earlier; ++ increments 1.
    break;
  case "Unknown":
    ++unknown; //This is the "no" int we made earlier; ++ increments 1.
    break;
}

In the end, all the ints will have the number of Unknown, Yes, and No.

Upvotes: 4

Related Questions