FileDocCategorySizeDatePackage
Observable.javaAPI DocAndroid 5.1 API2885Thu Mar 12 22:22:10 GMT 2015android.database

Observable

public abstract class Observable extends Object
Provides methods for registering or unregistering arbitrary observers in an {@link ArrayList}. This abstract class is intended to be subclassed and specialized to maintain a registry of observers of specific types and dispatch notifications to them.
param
T The observer type.

Fields Summary
protected final ArrayList
mObservers
The list of observers. An observer can be in the list at most once and will never be null.
Constructors Summary
Methods Summary
public voidregisterObserver(T observer)
Adds an observer to the list. The observer cannot be null and it must not already be registered.

param
observer the observer to register
throws
IllegalArgumentException the observer is null
throws
IllegalStateException the observer is already registered


                                              
        
        if (observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        }
        synchronized(mObservers) {
            if (mObservers.contains(observer)) {
                throw new IllegalStateException("Observer " + observer + " is already registered.");
            }
            mObservers.add(observer);
        }
    
public voidunregisterAll()
Remove all registered observers.

        synchronized(mObservers) {
            mObservers.clear();
        }
    
public voidunregisterObserver(T observer)
Removes a previously registered observer. The observer must not be null and it must already have been registered.

param
observer the observer to unregister
throws
IllegalArgumentException the observer is null
throws
IllegalStateException the observer is not yet registered

        if (observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        }
        synchronized(mObservers) {
            int index = mObservers.indexOf(observer);
            if (index == -1) {
                throw new IllegalStateException("Observer " + observer + " was not registered.");
            }
            mObservers.remove(index);
        }