0

I'm trying to make a method which returns int variable, but the method doesn't effect the variable.

This is my code:

int count;
public int getVotesForEvent(final String event_title) {
    firebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.child("Votes").exists()) {
                boolean flag = false;
                for (DataSnapshot event : dataSnapshot.child("Votes").getChildren()) {
                    String event_title2 = event.getKey();
                    if (event_title2.equals(event_title)) {
                        flag = true;
                        Toast.makeText(EventInfo.this, ""+Integer.parseInt(event.child("event_votes").getValue().toString()), Toast.LENGTH_SHORT).show();
                        count = Integer.parseInt(event.child("event_votes").getValue().toString());
                    }
                }
                if (flag != true) {
                    count = 0;
                }
            }
            else {
                count = 0;
            }
        }
    });
    return count;
}

The problem is that the line:

count = Integer.parseInt(event.child("event_votes").getValue().toString());

This line is not effecting the variable count.

What am I doing wrong here?

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
Gilad Neiger
  • 319
  • 1
  • 3
  • 14

1 Answers1

1

The method getVotesForEvent() itself does not call the code inside. So the value of count is not changed as your code returns from getVotesForEvent().

The methods only adds a ValueEventListener (as its name suggest :) ) with the defined behavior.

Only after an onDataChange event occurs, the code inside is performed.

There is no sense in returning count from getVotesForEvent(), it will just return the value which was there at the time you called that method, not when the value of count changes (sometimes in the future).

To repeat in other words: when calling addValueEventListener(), you are not executing the code it contains. You are only "loading" the code to be executed later, whenever the respective event occurs.

Think about your code as this:

int count;
public int getVotesForEvent(final String event_title) {
    firebaseRef.addValueEventListener(what-shall-be-done-sometimes-in-the-future-when-onDataChange-event-occurs);
    return count;
}

and here you clearly see that the value of count is not changed.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • Thanks for your explanation. But what changes do you recommend me to make in my code above? – Gilad Neiger Mar 23 '16 at 14:19
  • But op said he can see the Toast message inside that method, so the code is being executed. – user1301428 Mar 23 '16 at 14:20
  • Yes, It is getting in to the "if()". – Gilad Neiger Mar 23 '16 at 23:13
  • Have you tried what I had suggested in the comment? Change your onDataChange() method like this to test it: `public void onDataChange(DataSnapshot dataSnapshot) { count = 5; }` I mean remove all the other logic you have there and leave *only* the assignment to the count. Or you may have there `count++`. I still do not understand the meaning of returning `count` from `getVotesForEvent()`, it will just return the value which was there at the time you called *that* method, not when the value of `count` changes. – Honza Zidek Mar 24 '16 at 06:04