查看Android源码发现这个,决定记下下来。

1.在android.database这个包下面,存在这样一个抽象类DataSetObserver,里面包括onChanged()和onInvalidated()这个两个方法,用来监听数据的改变,在方法内要执行的操作。

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.database;

/**
 * Receives call backs when a data set has been changed, or made invalid. The typically data sets
 * that are observed are {@link Cursor}s or {@link android.widget.Adapter}s.
 * DataSetObserver must be implemented by objects which are added to a DataSetObservable.
 */
public abstract class DataSetObserver {
    /**
     * This method is called when the entire data set has changed,
     * most likely through a call to {@link Cursor#requery()} on a {@link Cursor}.
     */
    public void onChanged() {
        // Do nothing
    }

    /**
     * This method is called when the entire data becomes invalid,
     * most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a
     * {@link Cursor}.
     */
    public void onInvalidated() {
        // Do nothing
    }
}

2.同级目录中存在Observable这样一个类,如果将上面的DataSetObserver作为这个类的泛型参数。

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.database;

import java.util.ArrayList;

/**
 * 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.
 */
public abstract class Observable<T> {
    /**
     * The list of observers.  An observer can be in the list at most
     * once and will never be null.
     */
    protected final ArrayList<T> mObservers = new ArrayList<T>();

    /**
     * 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
     */
    public void registerObserver(T observer) {
        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);
        }
    }

    /**
     * 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
     */
    public void unregisterObserver(T observer) {
        if (observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        }
        synchronized(mObservers) {
            int index = mObservers.indexOf(observer);
            ) {
                throw new IllegalStateException("Observer " + observer + " was not registered.");
            }
            mObservers.remove(index);
        }
    }

    /**
     * Remove all registered observers.
     */
    public void unregisterAll() {
        synchronized(mObservers) {
            mObservers.clear();
        }
    }
}

Android源码学习(一) 数据集观察者的更多相关文章

  1. Android源码学习之装饰模式应用

    首先得了解最基础的装饰器模式 参考 设计模式之八 --- 装饰模式(Decorator) 参考链接:http://blog.csdn.net/cjjky/article/details/7478788 ...

  2. Android源码-学习随笔

    在线代码网站1:http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ 书籍: ...

  3. Android源码学习之模板方法模式应用

    一.模板方法模式定义 模板方法模式定义: defines the skeleton of an algorithm in a method, deferring some steps to subcl ...

  4. Android 源码学习

    工具篇:如何使用 Visual Studio Code 阅读 Android 源码:https://jekton.github.io/2018/05/11/how-to-read-android-so ...

  5. 【转】Android源码学习(2)使用Git和Repo进行版本管理

    原文网址:http://blog.chinaunix.net/uid-26074270-id-2458828.html Android项目采用Git和Repo进行版本管理.在大多数情况下,Git都可以 ...

  6. Android源码学习

    http://android-wheel.googlecode.com/svn/trunk/android-wheel-read-only 在github上面有一个叫做 android-wheel 的 ...

  7. Android学习系列(38)--Android源码下载和编译

    前面多篇文章介绍到如何下载和编译Android或者CM源码,不过一直都是放在<拓展系列>里.随着学习的深入,android源码是非常有参考和学习价值,强烈推荐大家都去下载,编译,学习,所以 ...

  8. (转)Android学习进阶路线导航线路(Android源码分享)

     转载请注明出处:http://blog.csdn.net/qinjuning 前言:公司最近来了很多应届实习生,看着他们充满信心但略带稚气的脸庞上,想到了去年的自己,那是的我是不是也和 现在的他们一 ...

  9. Android事件分发详解(三)——ViewGroup的dispatchTouchEvent()源码学习

    package cc.aa; import android.os.Environment; import android.view.MotionEvent; import android.view.V ...

随机推荐

  1. hdu 1010 Tempter of the Bone 深搜+剪枝

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. 【LeetCode练习题】Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  3. java 基础知识

    ...是Java代码的一部分,表明可以接受任意数量的对象,例子如下: 感觉很有趣 public static double max(double... values){ double largest ...

  4. DisplayMetircs 类

    Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的一项重要信息,很好的是,Android ...

  5. 使用ObjectAnimator开发打开、关闭书本动画

    动画效果 动画效果-分享链接 (想做成gif图的,尝试各种工具无果) ObjectAnimator简单介绍及实现思路 ObjectAnimator是从api level 11 (Android3.0x ...

  6. C# 基础知识 (一).概念与思想篇

    在C#中有一些我自己认为比较独特的知识点,这些知识点是我经常使用的知识,但对它们的了解还是比较少的,所以通过查找资料学习,总结了这些独特的知识点并简单叙述,第一篇主要是一些概念和思想方面的知识.(后面 ...

  7. CCTF-PWN1&&PWN3

    CCTF-PWN1&&PWN3 PWN1比较有意思,在得到输入的数据后使用shutdown将标准输入,标准输出和标准错误关闭了读写功能的读.也就是不能进行交互了,要保证一次输入就能拿到 ...

  8. input的样式简介

    <input type="text" autocomplete="off" placeholder="" x-webkit-speec ...

  9. HDU 1037 - Keep on Truckin'

    没啥可说的... #include <iostream> using namespace std; ; int a,b,c; int main() { while(cin>>a ...

  10. C++ Primer之 十二章 类

    1.关于类的const对象 const对象只能调用声明为const的成员函数,在这篇csdn博客中也讨论了这个问题. 究其原因是因为 const 对象A 调用了非const函数F, F因为没有cons ...