如何在android中监听SQLite数据库的变化

时间:2014-06-12 17:38:33

标签: android

我在android中使用SQLite数据库,并希望监听任何数据库更改。我怎么能这样做?

感谢您以后的所有帮助!

3 个答案:

答案 0 :(得分:4)

SQLite不提供任何更改侦听器功能;你必须自己监控它。实现这一目标的最简单方法是在修改数据库的任何时候发送广播(甚至更好的是LocalBroadcast)。一些数据库库已经提供了与此类似的功能 - 请查看GreenDAO

答案 1 :(得分:0)

事实上,SQLite提供了这样的功能:SQLite Data Change Notification Callbacks

如何在Android中使用它是另一个故事..

答案 2 :(得分:0)

Android上数据库的changeListener的简单实现

假设您有一个可在android应用中处理查询的类,我们需要使数据库方法可观察。 并且我们还需要一些听众来观察上述可观察的事物。让我们创建数据库处理程序observable

让我们创建可观察的界面

public interface DatabaseObservable {
    //register the observer with this method
    void registerDbObserver(DatabaseObserver databaseObserver);
    //unregister the observer with this method
    void removeDbObserver(DatabaseObserver databaseObserver);
    //call this method upon database change
    void notifyDbChanged();

}

现在implement您数据库类中的可观察对象

public class LocalStorageDb extends SQLiteOpenHelper implements DatabaseObservable {
LocalStorageDb lDb;

//make it Singleton
public static synchronized LocalStorageDB getInstance(Context context) {
    if (mlLocalQuickChatDB == null) {
        mlLocalQuickChatDB = new LocalStorageDB(context.getApplicationContext());
    }
    return mlLocalQuickChatDB;
}

//there are some methods to do some queries
    public void createContact(Foo foo, Bar bar){

//some queries here

//call the Observable Method to let know the observers that it has changed
onDatabaseChanged();
}

//now override the DatabaseObservable method which is responsible to notify the listeners
@Override
public void onDatabaseChanged() {
    for (DatabaseObserver databaseObserver:observerArrayList){
if (databaseObserver!= null){
    databaseObserver.onDatabaseChanged();
    }}
}
//also you need functions to **register** or **unregister** the observers:
 @Override
public void registerDbObserver(DatabaseObserver databaseObserver) {
    if (!observerArrayList.contains(databaseObserver)){
        observerArrayList.add(databaseObserver);
    }

@Override
public void removeDbObserver(DatabaseObserver databaseObserver) {
    observerArrayList.remove(databaseObserver);
}

然后我们需要一个observer观察更改:

public interface DatabaseObserver {
void onDatabaseChanged();
}

现在您的活动或片段中有一个函数来获取更改,例如getLocalContact。在片段上实现观察者,例如:

public class ExampleFragment extends Fragment implements DatabaseObserver {

LocalStorageDB localStorageDB;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    localStorageDB = LocalStorageDB.getInstance();
}

@Override
public void onPause() {
    super.onPause();
    localStorageDB.removeObserver(this);

}


@Override
public void onResume() {
    localStorageDB.registerObserver(this);
    super.onResume();
}

public ExampleFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_example, container, false);
}

@Override
public void onDatabaseChanged() {
getLocalContact();
}

private void getLocalContact(){
    //function to fetch contacts from database
}
}