Android - 游标加载器如何知道基础数据集已更改?
Android - How does cursor loader know underlying dataset has changed?
我正在尝试学习 Android 编程,并且一直在使用 Udacity Sunshine 应用程序。
有件事让我很困惑。我有一个内容提供者,它执行插入、删除、选择等操作,我有一个游标加载器,它也是一个片段,我有一个游标适配器,除了加载器如何知道数据已更改之外,大部分都是有意义的。
在内容提供者的更新方法中,它会执行此操作,我假设它是在通知某些数据已更改:
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
加载器在 onCreateLoader 方法中设置:
return new CursorLoader(
getActivity(),
weatherForLocationUri,
FORECAST_COLUMNS,
null,
null,
sortOrder
);
加载的 URI 与用于更新的 URI 不同,那么加载程序如何知道下面的数据已更改?
适配器在当前加载的游标上有一个句柄,因为它是在 onLoadFinished 中设置的:
mForecastAdapter.swapCursor(data);
最后,列表视图本身在游标适配器上有一个句柄:
mListView.setAdapter(mForecastAdapter);
我只是不明白加载程序如何通过这种机制知道数据已更改以及它如何通知适配器指示它重绘 UI?
另外,如果我对此有任何理解错误,请指正!
好吧,我不认为我会得到真正的机制,但正如 API 所述:
"The Loader will monitor for changes to the data, and report them to you through new calls here. You should not monitor the data yourself. For example, if the data is a Cursor and you place it in a CursorAdapter, use the CursorAdapter(android.content.Context, android.database.Cursor, int) constructor without passing in either FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0 for the flags argument). This prevents the CursorAdapter from doing its own observing of the Cursor, which is not needed since when a change happens you will get a new Cursor throw another call here."
我可以把它当作是为我完成的给定的,每当下面的数据发生变化时,都会触发对 onLoadFinished 的新调用,进而调用:
mForecastAdapter.swapCursor(data);
游标将自己注册为后代通知,这意味着当根 URI 被通知更改时,URI 的后代也会收到通知,后代可以是任何带有附加路径信息的东西。
我正在尝试学习 Android 编程,并且一直在使用 Udacity Sunshine 应用程序。
有件事让我很困惑。我有一个内容提供者,它执行插入、删除、选择等操作,我有一个游标加载器,它也是一个片段,我有一个游标适配器,除了加载器如何知道数据已更改之外,大部分都是有意义的。
在内容提供者的更新方法中,它会执行此操作,我假设它是在通知某些数据已更改:
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
加载器在 onCreateLoader 方法中设置:
return new CursorLoader(
getActivity(),
weatherForLocationUri,
FORECAST_COLUMNS,
null,
null,
sortOrder
);
加载的 URI 与用于更新的 URI 不同,那么加载程序如何知道下面的数据已更改?
适配器在当前加载的游标上有一个句柄,因为它是在 onLoadFinished 中设置的:
mForecastAdapter.swapCursor(data);
最后,列表视图本身在游标适配器上有一个句柄:
mListView.setAdapter(mForecastAdapter);
我只是不明白加载程序如何通过这种机制知道数据已更改以及它如何通知适配器指示它重绘 UI?
另外,如果我对此有任何理解错误,请指正!
好吧,我不认为我会得到真正的机制,但正如 API 所述:
"The Loader will monitor for changes to the data, and report them to you through new calls here. You should not monitor the data yourself. For example, if the data is a Cursor and you place it in a CursorAdapter, use the CursorAdapter(android.content.Context, android.database.Cursor, int) constructor without passing in either FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0 for the flags argument). This prevents the CursorAdapter from doing its own observing of the Cursor, which is not needed since when a change happens you will get a new Cursor throw another call here."
我可以把它当作是为我完成的给定的,每当下面的数据发生变化时,都会触发对 onLoadFinished 的新调用,进而调用:
mForecastAdapter.swapCursor(data);
游标将自己注册为后代通知,这意味着当根 URI 被通知更改时,URI 的后代也会收到通知,后代可以是任何带有附加路径信息的东西。