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) {
FGH
  • 2,900
  • 6
  • 26
  • 59
  • @AlexMamo: while the [question](https://stackoverflow.com/questions/51861689/how-getchildrencount-works-in-the-background) you linked may be relevant to this question, I don't think it's a duplicate. While both are about counting nodes, this question seems to require some test on the child nodes. – Frank van Puffelen Sep 11 '19 at 14:20
  • what's the result that you're looking for? So: given the JSON that you show, what values/output do you expect from the code? – Frank van Puffelen Sep 11 '19 at 14:21
  • @FrankvanPuffelen Ok puf, thanks again for reopening the question. – Alex Mamo Sep 11 '19 at 14:21
  • Please tell us what @FrankvanPuffelen asked for and sorry for closing the question ealier. – Alex Mamo Sep 11 '19 at 14:23
  • @AlexMamo Please give me the solution – FGH Sep 11 '19 at 15:38
  • @SivanathanPrasath Please provide the informations that Frank van Puffelen asked for. – Alex Mamo Sep 11 '19 at 15:42
  • @FrankvanPuffelen please see the image https://i.stack.imgur.com/IrQny.png – FGH Sep 11 '19 at 15:47

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
  • 130,605
  • 17
  • 163
  • 193
  • 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 – FGH 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! – FGH 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