1

I need to count the number of item where the attribute is equal to some String value in Firebase. May I know how the way to count it?

The way i did it, it keeps appear Index out of Bound, size:0 .

I want to count something like how many data in my table is equal to "normal" or "mild" or other word. For example, in the picture , there is two normal, hence, it should returns 2.

Thank you :)

Here is my firebase table table

Main Activity .class

public class MainActivity extends AppCompatActivity {
    RecyclerView rv;
    TextView title;
    DatabaseReference databaseReference;
    MyRecyclerViewPer adapter;
    ArrayList<Integer > nu;
    int r=0;
    ArrayList<Integer> one;
    ArrayList<Integer> two;
    ArrayList<Integer> three;
    ArrayList<Integer> four;
    Integer wow;
    int amild = 0,dmmild=0,dm=0,dpmild=0,smild=0;
    int amod = 0,dpmod=0,smod=0;
    int asev = 0,dpsev=0,dpvsev=0,ssev=0,svsev=0;
    int anor = 0,bnor=0,bpositive=0,dmnor=0,dpnor=0,snor=0;
    double per=0;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hori);

        title = findViewById(R.id.name);
        title.setText("Statistical Report");
        rv = findViewById(R.id.myRecycle);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        nu = new ArrayList<Integer>();
        one = new ArrayList<Integer>();
        two= new ArrayList<Integer>();
        three= new ArrayList<Integer>();
        four= new ArrayList<Integer>();

        final DatabaseReference rootRef= FirebaseDatabase.getInstance().getReference();
        databaseReference= rootRef.child("assess");
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//anxiety result

                if(dataSnapshot.child("anx").exists()){

                    for (DataSnapshot dataSnapshot1 : dataSnapshot.child("anxiety").getChildren()) {
                         wow= (int)dataSnapshot1.getChildrenCount();
                        nu.add(wow);
                        String key =dataSnapshot1.getKey();

                        DatabaseReference userRef = rootRef.child("assess").child("anx").child(key);
                        userRef.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot2) {

                                for (DataSnapshot d : dataSnapshot2.getChildren()) {
//how many row equal to mild condition

                                    if (d.getValue(AssessmentResult.class).getCon().equals("Mild")) {
                                        amild++;
                                    } 
//how many row equal to moderate condition
else if (d.getValue(AssessmentResult.class).getCon().equals("Moderate")) {
                                        amod++;
                                    } 
//how many row equal to severe condition
else if (d.getValue(AssessmentResult.class).getCon().equals("Severe")) {
                                        asev++;
                                    } 
//how many row equal to normal condition
else if(d.getValue(AssessmentResult.class).getCon().equals("Normal")){
                                        anor++;
                                    }

                                }
                                if(amild==0){
                                    one.add(0);
                                }else{
                                    one.add(anor);
                                }
                                if(amild==0){
                                    two.add(0);
                                }else{
                                    two.add(amild);
                                }
                                if(amod==0){
                                    three.add(0);
                                }else{
                                    three.add(amod);
                                }
                               if(asev==0){
                                   four.add(0);
                               }else{
                                   four.add(asev);
                               }


                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
                else{
                    nu.add(0);
                }


                adapter = new MyRecyclerView(MainActivity.this, nu,one,two,three,four,five);
                rv.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }


}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Kim E. R
  • 73
  • 1
  • 8
  • Firebase Realtime Database doesn't have a count operation. You will have to query the database for all the matching children, then use the size of the result set as the count. – Doug Stevenson Oct 30 '19 at 06:27

0 Answers0