ContentObserver SQLite的? [英] ContentObserver for SQLite?

查看:120
本文介绍了ContentObserver SQLite的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到如何在一个ListView显示来自我的DB数据同时跟踪变化的数据库。

I've been looking into ways to show data from my DB in a ListView while keeping track of changes in the database.

让我们说我有一个聊天应用程序,显示了所有我的一个成员的聊天室的一个ListView

Let's say I have a chat app that shows a ListView of all the chatrooms I am a member of.

适配器的查询是 SELECT * FROM CHAT_ROOM ORDER BY UPTDATE_TIME ,这意味着我要首先显示最近活动的聊天室。因为我在 ChatroomListActivity 接收到聊天室3号的消息,这意味着它需要成为现在聊天室头号并据此重新排列的单元格。

the query for the adapter is SELECT * FROM CHAT_ROOM ORDER BY UPTDATE_TIME, meaning I want the chatrooms with recent activity to be shown first. as I am in the ChatroomListActivity a message is received for chatroom number 3, this means it needs to become now chatroom number one and rearrange the cell accordingly.

我知道,装载机能够重新查询在后台但commonsware在我的 previous问题回答这里,好像装载机是不是这个不够好。

I know that Loaders are capable of re-querying in the background but as commonsware answered in my previous question here, it seems like Loaders are not good enough for this.

是有实时观察数据库的变化不同的机制?

is there a different mechanism for observing database changes in realtime?

推荐答案

我做它的方式


  • 类GroupChatActivity

  • 类MessageDataSource

  • 类HandleNewMessage

在群聊活动是有点明显。
MessageDataSource中插入,然后从数据库返回的对象的类。
HandleNewMessage,不认为我需要解释这一点。

The GroupChat Activity is kinda obvious. MessageDataSource the class that inserts and returns objects from the DB. HandleNewMessage, dont think I need to explain this one.


  • GROUPCHAT - > MessageDataSource - >的getMessages()

  • HandleNewMessage - > MessageDataSource - > insertNewMessage();

笏现在想的是,群组聊天和放大器; HandleNewMessage告知MessageDataSource的同一个实例。
所以,你想要做的是使静态参考与在MessageDataSource到MessageDataSource
例如:

wat you now want is that the GroupChat & HandleNewMessage talk with the same instance of MessageDataSource. So you what you want to do is make a static reference to MessageDataSource with in the MessageDataSource Example:

 public class MessageDataSource {

    private static MessageDataSource mInstance;

    public static MessageDataSource getInstance(Context context) {
        if(mInstance == null){
            mInstance = new MessageDataSource(context);
        }
        return mInstance;
    }

    private MessageDataSource(Context context) {
        // NOTE: private constructor
    }
}

由于你让你的私人的构造函数,你不能这样做。

Because you make your constructor private you can't do

MessageDataSource data = new MessageDataSource(context);

由于构造函数是私有的,你必须做的。

Because the constructor is private you must do

MessageDataSource data = MessageDataSource.getInstance(context);

现在你的类的 GroupActivity HandleNewMessage 能谈谈你MessageDataSource的同一个实例可以使一个自定义的接口的通知任何观察听众

now your classes GroupActivity and HandleNewMessage can talk to the same instance of your MessageDataSource you can make an custom Interface that notifies any observing listener.

例如:

    public class MessageDataSource {

    private static MessageDataSource mInstance;

    private SQLiteDatabase mDataBase;
    private SQLiteHelper DBHelper;
    private OnInsert mOnInsert;

    public static MessageDataSource getInstance(Context context) {
        if(mInstance == null){
            mInstance = new MessageDataSource(context);
        }
        return mInstance;
    }

    private MessageDataSource(Context context) {
        DBHelper = SQLiteHelper.getInstance(context);
    }

    public void createMessage(String JID, String message, String sender) {
        // Do DB Stuff here
        if(mOnInsert != null) {
            mOnInsert.onInsert(message);
        }
    }


    public void setOnInsertListener(final OnInsert onInsert) {
        mOnInsert = onInsert;
    }

    public static interface OnInsert {
        // Notify the observer that an insert has bin made
        public String onInsert(String message);
    }
}

您可以通过你觉得你需要一个界面发送尽可能多的对象。
现在,在您的 GroupChatActivity 您可以设置(对我来说)一个OnInserListener正常的方式,你会设置一个OnClickListener。或者你可以让你的活动实现的接口。

You can send as many Objects through a interface you think you require. Now in your GroupChatActivity you can set (in my case) a OnInserListener the normal way you would set an OnClickListener. Or you can let you Activity Implement the Interface.

这个例子也莫比不符合你的实际需要,但一些新的东西插入到您DB观察,你需要更新你的ListView,这在我看来是要走的路,但如果有人发现了一个更好的办法请给出意见。 :D

This example does maby not qualify for you exact needs, but to observe when something new is inserted into you DB and you need to update your listView, this is in my opinion the way to go, but if someone finds a better way please comment. :D

我希望这可以让你在正确的方向。

I hope this can get you in the right direction.

亲切的问候。

这篇关于ContentObserver SQLite的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆