是的,您可以编写一个自定义加载程序,当您将数据保存在数据库中时,您必须手动通知数据库更改。为此,您可以使用广播接收器、绿色机器人事件总线等。请参阅下面的代码
自定义消息加载器类在收到事件总线通知时加载数据。
MessageListLoader.java
public class MessageListLoader extends AsyncTaskLoader<List<Message>> {
private List<Message> mMessages;
private long mGroupId;
private Context mContext;
public MessageListLoader(Context context, long groupId) {
super(context);
mGroupId = groupId;
}
private IMobileService getMobileService() {
return MobileServiceImpl.getInstance(mContext);
}
@Override
public List<Message> loadInBackground() {
return getMobileService().getMessagesByGroupId(mGroupId);
}
@Override
public void deliverResult(List<Message> newMessageList) {
if (isReset()) {
mMessages = null;
return;
}
List<Message> oldMessageList = mMessages;
mMessages = newMessageList;
if (isStarted()) {
super.deliverResult(newMessageList);
}
// Invalidate the old data as we don't need it any more.
if (oldMessageList != null && oldMessageList != newMessageList) {
oldMessageList = null;
}
}
/**
* The OnEvent method will called when new message is added to database.
*
* @param event
*/
@Subscribe
public void onEvent(NewMessageEvent event) {
// reload data from data base
forceLoad();
}
@Override
protected void onStartLoading() {
if (mMessages != null) {
// If we currently have a result available, deliver it
// immediately.
deliverResult(mMessages);
}
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
}
@Override
protected void onReset() {
mMessages = null;
EventBus.getDefault().unregister(this);
}
}
移动服务类用于提供所有与数据库相关的服务。
MobileServiceImpl.java
public class MobileServiceImpl implements IMobileService {
private static final String TAG = "MobileServiceImpl";
private static final String DATABASE_NAME = "demo.db";
private static IMobileService instance = null;
private DaoSession mDaoSession;
private MobileServiceImpl(Context context) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
mDaoSession = daoMaster.newSession();
}
public static IMobileService getInstance(Context context) {
if (instance == null) {
instance = new MobileServiceImpl(context);
}
return instance;
}
private MessageDao getMessageDao() {
return mDaoSession.getMessageDao();
}
/**
* The saveMessage() method is used to save given message into database.
*
* @param message Specifies the message object to be saved.
* @param notifyUi Specifies the boolean flag to notify the change in database to ui.
* @return Saved message id.
*/
@Override
public long saveMessage(Message message, boolean notifyUi) {
long id = getMessageDao().insert(message);
if (notifyUi)
EventBus.getDefault().post(new NewMessageEvent(id));
return id;
}
@Override
public List<Message> getMessagesByGroupId(long groupId) {
return getMessageDao()
.queryBuilder()
.where(MessageDao.Properties.GroupId.eq(groupId))
.orderDesc(MessageDao.Properties.Id).list();
}
@Override
public Message getMessageById(long messageId) {
return getMessageDao().load(messageId);
}
}
从这里下载示例项目