0

How can I return a value that I got from adding through loop inside a function?
It says cannot assign a value to final variable total_from_loop
I tried transforming it into a final one element array but it only returns 0 value

 public int getCount(String x){
               final int total_from_loop = 0;
               final Query q = myDb.child(currentDate).orderByChild("to_search").equalTo(x);
               q.addValueEventListener(new ValueEventListener() {
                   @Override
                   public void onDataChange(DataSnapshot dataSnapshot) {
                       //this is where I get the total
                       for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
                           total_from_loop += Integer.parseInt(zoneSnapshot.child("total").getValue().toString());
                       }
                   }
                   @Override
                   public void onCancelled(DatabaseError databaseError) {
                       Toast.makeText(Activity.this, databaseError.toException().toString(), Toast.LENGTH_LONG).show();
                   }
               });
               //return the total from loop
               return total_from_loop;
           }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

once a final variable is initialized, it cannot be changed

KKKKeZZZZ
  • 11
  • 5