I use the MaterialDrawer library to create a simple box for my application, some of the class instances in the library need the string passed to them when called. An example is the IProfile class:
IProfile profile = new ProfileDrawerItem().withName("John Doe");
where the withName() method takes a string.
I created the class MyObservable.java (extends Observable), which I intend to use to get the data that will be used in my MainActivity , which implements the MaterialDrawer library. In this class, I have an implementData() method that my listener has for what I need from my firebase database.
It looks like this:
public class MyObservable extends Observable {
Then I notify my watchers of the username change using the changeUsername() method:
public void changeUsername(String username) { this.username = username; setChanged(); notifyObservers(username); }
In the MyObservable class, I have a MyObserver class that implements Observer using the update() method, called whenever the observer is updated. In the update() method, for now, I just print the username user to make sure something is really happening.
Here I need data from the observer (in MainActivity):
public class MainActivity extends AppCompatActivity { ... public void appDrawer(){ IProfile profile = new ProfileDrawerItem()
I spent hours trying to " respond " to events happening in the listener, trying to extract the data that will be used in my MainActivity, but I'm not sure that I will use the Observable / Observer Pattern correctly, also, since this is an asynchronous Firebase event to receive data, using Observer was the best way to do this.
The appDrawer() method is called in my onCreate() activity.
How to get data from a listener using Observer so that I can use it elsewhere?