Java – how to implement observer to get data from listener?

I am using the materialdrawer library to create a simple drawer for my application. Some class instances in the library need to pass strings to them when calling An example is the iprofile class:

IProfile profile = new ProfileDrawerItem(). withName(“John Doe”);

The withname () method accepts a string

I created a class myobservable Java (extended observable) class. I intend to use it to obtain the data to be used in my mainactivity. This implements the materialdrawer library In this class, I have a method implementdata (), which has the listener required by my firebase database

This is what it looks like:

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 observer of the user name change through the changeusername() method:

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

        setChanged();
        notifyObservers(username);
    }

In the myobservable class, I have a myobserver class, which implements observer and calls the update () method when updating the observer In the now () method, I just print out the user's user name to make sure what actually happened

This is the data I need from the observer (in mainactivity):

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

    ...

}

I spent several hours trying to "react" to the events in the listener by trying to retrieve the data to be used in my mainactivity, but I'm not sure if I used the observable / observer mode correctly, because this is an asynchronous firebase event to obtain data, and using observer is the best way

The appDrawer () method is called in the onCreate () of my Activity.

How do I use observer to retrieve data from the listener so that I can use it elsewhere?

Solution

I don't know what happened to your design Naming a class listener and making it observable seems counterintuitive The audience listens or observes Nevertheless, it sounds like you have another class in listener that is observer, so I'm a little lost, but you don't seem to be sure whether you have implemented the pattern correctly I can clean it up 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 and the parameters you pass to notifyobservers () (the data you want to share with the observer) If you do, you should get the behavior you expect

As you can see, data can be sent to observers outside or inside observable objects When you run it, the output is

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>