Retrieve Java object by ID from Firebase
<p>I am writing an Android app and I am trying to retrieve an object of the class <code>User.java</code> by ID from its Firebase pertinent table. I would like to know how to get it from Java side, as long as I tried the examples stated in <a href="https://www.firebase.com/docs/android/guide/retrieving-data.html" rel="nofollow">Firebase Official docs </a>but none of them is working for me.</p>

<p>Taking this SO question as example, I want a method with the following interface:</p>

<pre><code>public User readUser(String userId);
</code></pre>

<p>In other words, I want to execute:</p>

<p><code>readUser(-lnnROTBVv6FznK81k3n)</code> </p>

<p>and retrieve the associated <code>User</code> object </p>

<p>Thanks</p>

<p><strong>--------------------------------------------------------------EDIT--------------------------------------------------------------</strong>: </p>

<p>I managed to get the value with this code:</p>

<pre><code>public void retrieveUser(final String email){
        firebaseUsersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
                    if(messageSnapshot.getKey().equals(Email.encodeID(email))){

                        retrievedUser = messageSnapshot.getValue(User.class);
                        break;
                    }
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) { }
        });
    }
</code></pre>

<p>Please not retrievedUser is a class <code>attribute</code>, thus a <code>field</code>. I am accessing that field from the code, but even I see it takes the value on the debugger, <strong>it is being null on the calling code</strong>. </p>

<p>Any hint? Can´t I just return it in the method itself, so it would be?:</p>

<pre><code>public User retrieveUser(final String email);
</code></pre>

<p>Thanks</p>