1

my firebasebase database structure

I created one android application in this application, I want to count unique child value.

I created for loop inside the database reference and assign global variable but the thing is I don't know how many unique values will become to my calculation(HOw many variables).

dbcloud.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
    {
        Modelfortotalsoldcount mc=dataSnapshot1.getValue(Modelfortotalsoldcount.class);
        String name=mc.getFoodname();
        String qty=mc.getFoodqty();
        String price=mc.getFoodprice();
        int totalcount=0;
        if(fname.equals(name))
        {
            String fgname=name;
            String fgprice=price;
            Integer intqty=Integer.valueOf(qty);
            totalcount=totalcount+intqty;
            String totalval=String.valueOf(totalcount);
        }
    }
  }
  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {
Prasath
  • 1,374
  • 4
  • 13
  • 38

1 Answers1

3

Assuming that all those objects are direct children of your Firebase root, to solve this, please use the following query:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.orderByChild("foodname").equalTo("Burger");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int count = 0;
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String foodqty = ds.child("foodqty").getValue(String.class);
            count = count + Integer.valueOf(foodqty);

        }
        Log.d(TAG, "count:" + count);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be:

count: 4

Edit:

If you don't know the food name then you should use a variable instead:

Query query = rootRef.orderByChild("foodname").equalTo(foodName);

In which the foodName is what the user types in a EditText for example:

String foodName = foodNameEditText.getText().toString();
Alex Mamo
  • 112,184
  • 14
  • 139
  • 167
  • Thank you! such a big help, This food names(Burger, Coke, Chickenburger) will be generated by the users so I don't know the users' food name if I get the user's food name into the line [Query query = rootRef.orderByChild("foodname").equalTo("Burger");] , so i need to create count variable name for every foodname,how can i create count variable for future foodname's ? Thank you sir – Prasath Sep 11 '19 at 18:45
  • In that case, please see my updated answer. Does it work this way? – Alex Mamo Sep 11 '19 at 18:50
  • Thank you sir, How can I create a count variable name for the foodname , because i want to count every unique foodname ? ,so i want to create unique foodname ? can you tell me a solution, Please Thank you! – Prasath Sep 11 '19 at 18:57
  • This sounds basically as another question. In order to follow the rules of this community, please post another fresh question, so me and other Firebase developers can help you. – Alex Mamo Sep 11 '19 at 19:06