How to implement Observer to get data from a listener?

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 { // 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); } } 

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 need the user data here .withName("Test") .withEmail(" test@test.com ") .withIcon(R.drawable.avatar2); } ... } 

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?

+5
source share
1 answer

I can’t tell you what really happens in your design. Naming the Listener class and then its Observable seems counterintuitive. The listener is listening or observing. However, it looks like you have another class in the Listener , which is Observer , so I'm a little lost, but you don't seem to be sure if you implemented the template correctly. I can clarify this with an example.

 public class MyObserver implements Observer { @Override public void update(Observable o, Object arg) { System.out.println(arg + " " + ((MyObservable)o).name); } } public class MyObservable extends Observable { public String name = "Observable"; public void changeMe(String word) { setChanged(); notifyObservers(word); } } public class Test { public static void main(String[] args) { MyObservable myObservable = new MyObservable(); myObservable.addObserver(new MyObserver()); myObservable.changeMe("Hello"); } } 

The update method gives you the object you are observing, as well as the arguments (the data that you want to use with the observer) that you passed to notifyObservers() . If you did it this way, then you should get the expected behavior.

As you can see, data can be sent to observers located outside or inside the observed. When you run it, the output will be ...

Hi watched

+13
source

Source: https://habr.com/ru/post/1241856/


All Articles