How do I implement an Observer to get data from a listener?
<p>I am using the <a href="https://github.com/mikepenz/MaterialDrawer" rel="noreferrer">MaterialDrawer</a> library to create a simple drawer for my app, some of the instances of classes in the library need a string passed into them when called. An example is the <code>IProfile</code> class:</p>

<p><code>IProfile profile = new ProfileDrawerItem().withName("John Doe");</code></p>

<p>where the <code>withName()</code> method takes in a string.</p>

<p>I have created a class <code>MyObservable.java</code> (extends Observable) class that I intend using to get data to be used in my <code>MainActivity</code> which has the MaterialDrawer library implemented. In this class, I have a method <code>implementData()</code> which has my listener for what I need from my firebase database.</p>

<p>This is what it looks like: </p>

<pre><code>    public class MyObservable extends Observable {

    // Attach our firebase reference...
    Firebase userRef = new Firebase("https://myApp.firebaseio.com/users");
    AuthData authData;

    public String username = "";
    public String email = "";


    public void changeUsername(String username) {
        this.username = username;

        setChanged();
        notifyObservers(username);
    }


    public void implementData(){
        // Get the authentication data of our user from the reference.
        authData = userRef.getAuth();

        // This is the listener I have to get the data from. 
        userRef.child(authData.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {

                UserSchema user = snapshot.getValue(UserSchema.class);
                // This guy needs to be used in the MainActivity appDrawer() method
                String userName = user.getName();
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

    }
}

class MyObserver implements Observer {

    public void update(Observable observable, Object data) {
        // For now I just print it out to System.out
        System.out.println("Username:" + data);
    }
}
</code></pre>

<p>Then I notify my observers of the username change with the <code>changeUsername()</code> method:</p>

<pre><code>public void changeUsername(String username) {
        this.username = username;

        setChanged();
        notifyObservers(username);
    }
</code></pre>

<p>In the MyObservable class, I have a <code>MyObserver</code> class that implements <code>Observer</code> with the <code>update()</code> method called whenever an observer has been updated. In the <code>update()</code> method for now, I just print out the <code>username</code> of the user to ensure something is actually happening.</p>

<p>This is where I need the data from the observer (In the MainActivity):</p>

<pre><code>public class MainActivity extends AppCompatActivity {
    ...

    public void appDrawer(){

        IProfile profile = new ProfileDrawerItem()

                // I need the user's data here
                .withName("Test")
                .withEmail("test@test.com")
                .withIcon(R.drawable.avatar2);
    }

    ...

}
</code></pre>

<p>I have spent hours trying to '<strong>react</strong>' to the events happening in the listener by trying to retrieve data to be used in my MainActivity but I'm not sure I'm using the Observable/Observer pattern properly, also since this is an Asynchronous event by Firebase to get data, using an Observer has been the best way to do this.</p>

<p>The <code>appDrawer()</code> method is called in my Activity's <code>onCreate()</code> .</p>

<p>How do I retrieve data from the Listener with an Observer, so I can use it elsewhere? </p>