Firebase value event listener not working

Two things come to mind from this question, one about Firebase listeners and the second about removing excess code.

A fundamental thing about Firebase listeners is that they are asynchronous, meaning your code does not wait for the result before executing the next line. So look at the comments in this skeleton code:

userRef.child("id").
addSingleValueEventListener(new ValueEventListener() {
    @Override public void onDataChange (DataSnapshot dataSnapshot){
        // code here does not get executed straight away,
        // it gets executed whenever data is received back from the remote database
    }

    @Override public void onCancelled (DatabaseError databaseError){

    }
});
// if you have a line of code here immediately after adding the listener
// the onDataChange code *won't have run*. You go straight to these lines
// of code, the onDataChange method will run whenever it's ready.

So this means that if you want to do something with the data you are getting in onDataChange you should put that code inside the onDataChange method (or some other method called from there or in some other way running that code after the data has been delivered back).

Regarding the second part, a slightly more Firebasey way of checking for existence of an int and getting the value would be:

@Override public void onDataChange (DataSnapshot dataSnapshot){
    if (dataSnapshot.exists()) {
        id = dataSnapshot.getValue(Integer.class);
    } else {
        id = 1;
    }
}