Android LoaderManager 和 CursorLoader 混淆
我正在尝试将我的 Android 应用程序转换为使用 LoaderManager 和 CursorLoader。基本上,我有一个包含 ADDRESS 列和 DISTANCE 列的 SQLite 数据库,我想将列值加载到我的 ListView 行中。
现在,我已经做了很多研究,一切都指向本教程: http ://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
这是一个很好的教程,但有一些事情我仍然不明白。主要是,如何构造传递到“new CursorLoader()”的内容 URI?我没有使用任何外部数据,例如设备联系人等。
请参阅下面的代码。我对如何生成 BASE_URI 的值感到困惑:
public class FavoritesFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter mAdapter;
static final String[] FAVORITES_SUMMARY_PROJECTION = new String[] {
MyApplication.COLUMN_ID, MyApplication.COLUMN_ADDRESS,
MyApplication.COLUMN_DISTANCE, };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.locations_list_row, null, new String[] {
MyApplication.COLUMN_ADDRESS,
MyApplication.COLUMN_DISTANCE }, new int[] {
R.id.address2, R.id.distance }, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), ***BASE_URI***,
FAVORITES_SUMMARY_PROJECTION, null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
I am trying to convert my Android application to use the LoaderManager and CursorLoader. Basically, I have an SQLite database containing an ADDRESS column and a DISTANCE column, and I want to load the column values into my ListView rows.
Now, I have done a lot of research, and everything points to this tutorial: http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
It's a nice tutorial, but there are a couple of things that I still don't understand. Mainly, how do I construct the content URI that gets passed into 'new CursorLoader()'? I'm not using any external data such from the device Contacts, etc.
Please see my code below. I am confused as to how to generate the value for BASE_URI:
public class FavoritesFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter mAdapter;
static final String[] FAVORITES_SUMMARY_PROJECTION = new String[] {
MyApplication.COLUMN_ID, MyApplication.COLUMN_ADDRESS,
MyApplication.COLUMN_DISTANCE, };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.locations_list_row, null, new String[] {
MyApplication.COLUMN_ADDRESS,
MyApplication.COLUMN_DISTANCE }, new int[] {
R.id.address2, R.id.distance }, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), ***BASE_URI***,
FAVORITES_SUMMARY_PROJECTION, null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BASE_URI 应该是在
ContentProvider
中定义的静态 Uri
,它的用途是当您对ContentProvider
进行查询/更新/插入/任何操作时>,同样在ContentProvider
中定义的UriMatcher
可以输出一个Integer
,允许您使用Switch
语句(如示例中的该教程中的query()
方法)来为数据库中的正确表设置正确的查询。您应该为数据库中的每个表定义不同的BASE_URI
。如果您查看该教程,他们在
ContentProvider
中定义了一个Uri
:在您的 ContentProvider 中,您应该将
TUTORIALS_BASE_PATH
的值从 < code>"tutorials" 为包含您提到的 ADDRESS 和 DISTANCE 列的表名称。您的CursorLoader
构造函数代码如下所示:为了完整起见,您应该将变量名称更改为更具描述性,而不是
TUTORIALS_BASE_PATH
和CONTENT_URI
,您应该将其更改为LOCATIONS_BASE_PATH
和LOCATIONS_URI
之类的内容。BASE_URI should be a
static Uri
defined in yourContentProvider
, it is used so that when you make a query/update/insert/whatever to theContentProvider
, aUriMatcher
which is also defined in theContentProvider
can output anInteger
allowing you to use aSwitch
statement (as in the example of thequery()
method in that tutorial) to setup the right query to the correct table in your database. You should define a differentBASE_URI
for each table in your database.If you look at that tutorial they have defined a single
Uri
in theContentProvider
:In your ContentProvider, you should change the value of
TUTORIALS_BASE_PATH
from"tutorials"
to whatever the name of your table is that contains the ADDRESS and DISTANCE columns that you mentioned. YourCursorLoader
constructor code would look like this:For completeness, you should change the variable names to be more descriptive, so rather than
TUTORIALS_BASE_PATH
andCONTENT_URI
, you should change it to something likeLOCATIONS_BASE_PATH
andLOCATIONS_URI
.