简体   繁体   English

Observable / Observer不工作?

[英]Observable/Observer not working?

I tried implementing a static Observable in my Application subclass, but it's not working. 我尝试在我的Application子类中实现静态Observable,但它不起作用。 No exceptions or error messages, but my update() callback doesn't get called. 没有异常或错误消息,但我的update()回调没有被调用。

MyApplication.java MyApplication.java

public class MyApplication extends Application{
    public static Observable appObserver = new Observable();

    public void onCreate(){
        super.onCreate();        
    }

}

Foo.java Foo.java

MyApplication.appObserver.notifyObservers( "Hello world" );

BarFragment.java BarFragment.java

public class BarFragment extends Fragment implements Observer{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);                
        MyApplication.appObserver.addObserver(this);
    }


    @Override
    public void onDestroy() {       
        MyApplication.appObserver.deleteObserver(this);
        super.onDestroy();
    }


    @Override
    public void update(Observable observable, Object data) {
        Log.i("BarFragment", data.toString()); // Should log, but doesn't
    }

}

What's more, I tried scribbling down a simple Observable of my own, and then everything worked like a charm with just substituting public static Observable appObserver = new Observable(); 更重要的是,我试着在我自己的一个简单的Observable上public static Observable appObserver = new Observable(); ,然后一切都像一个魅力一样,只需要替换public static Observable appObserver = new Observable(); with public static MyObservable appObserver = new MyObservable(); with public static MyObservable appObserver = new MyObservable();

MyObservable.java MyObservable.java

public class MyObservable {
    protected List<Object> observers = new ArrayList<Object>();

    public void addObserver(Object observer){
        observers.add(observer);
    }

    public void notifyObservers(Object data){
        for( int i=0; i<observers.size(); i++ ){
            ((Observer) observers.get(i)).update(null, data);
        }
    }
}

What am I missing? 我错过了什么?

I'm testing this on a Nexus One with Android 2.3.6 if it matters. 我正在使用Android 2.3.6的Nexus One进行测试,如果重要的话。

Seems I missed a crucial part of the Observer mechanism, apparently one has to call setChanged() before notifyObservers() . 似乎我错过了Observer机制的一个关键部分,显然必须在notifyObservers()之前调用setChanged() notifyObservers() But the thing is, setChanged() is protected and thus only visible to any subclass of Observable. 但问题是, setChanged()受到保护,因此只对Observable的任何子类可见。

So I guess it's not meant to be used in the fashion I tried to use it, but the hackish subclass below will work... 所以我想这并不意味着我试图使用它的方式,但下面的hackish子类将工作...

public class MyObservable extends Observable{

    @Override
    public boolean hasChanged() {
        return true; //super.hasChanged();
    }

}

BadCash's answer actually didn't work for me, but my issue did stem from the same problem with Observable . BadCash的答案实际上对我不起作用,但我的问题确实源于Observable的同样问题。 Perhaps you're using an older implementation, but I'm on Java 8. Rather than overriding hasChanged() , I had to override notifyObservers() to always set itself to be changed before notifying: 也许你正在使用较旧的实现,但我使用的是Java 8.而不是重写hasChanged() ,我必须重写notifyObservers()以在通知之前始终将自己设置为更改

class WorkingObservable extends Observable{
    @Override
    public void notifyObservers(){
        setChanged();
        super.notifyObservers();
    }
};

That did the trick for me. 这对我有用。 I looked at the source code and it seems that the Observable class doesn't call hasChanged() method internally to decide whether to notify. 我查看了源代码,似乎Observable类没有在内部调用hasChanged()方法来决定是否通知。 It just checks the changed private variable. 它只是检查changed私有变量。 This probably could have been implemented better. 这可能可以更好地实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM