Android LoaderManager 和 CursorLoader 混淆

发布于 2025-01-07 00:05:41 字数 1804 浏览 1 评论 0原文

我正在尝试将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2025-01-14 00:05:41

BASE_URI 应该是在 ContentProvider 中定义的静态 Uri,它的用途是当您对 ContentProvider 进行查询/更新/插入/任何操作时>,同样在 ContentProvider 中定义的 UriMatcher 可以输出一个 Integer ,允许您使用 Switch 语句(如示例中的该教程中的 query() 方法)来为数据库中的正确表设置正确的查询。您应该为数据库中的每个表定义不同的BASE_URI

如果您查看该教程,他们在 ContentProvider 中定义了一个 Uri

private static final String TUTORIALS_BASE_PATH = "tutorials";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + TUTORIALS_BASE_PATH);

在您的 ContentProvider 中,您应该将 TUTORIALS_BASE_PATH 的值从 < code>"tutorials" 为包含您提到的 ADDRESS 和 DISTANCE 列的表名称。您的 CursorLoader 构造函数代码如下所示:

return new CursorLoader(getActivity(), YourContentProvider.CONTENT_URI,
            FAVORITES_SUMMARY_PROJECTION, null, null, null);

为了完整起见,您应该将变量名称更改为更具描述性,而不是 TUTORIALS_BASE_PATHCONTENT_URI ,您应该将其更改为 LOCATIONS_BASE_PATHLOCATIONS_URI 之类的内容。

BASE_URI should be a static Uri defined in your ContentProvider, it is used so that when you make a query/update/insert/whatever to the ContentProvider, a UriMatcher which is also defined in the ContentProvider can output an Integer allowing you to use a Switch statement (as in the example of the query() method in that tutorial) to setup the right query to the correct table in your database. You should define a different BASE_URI for each table in your database.

If you look at that tutorial they have defined a single Uri in the ContentProvider:

private static final String TUTORIALS_BASE_PATH = "tutorials";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + TUTORIALS_BASE_PATH);

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. Your CursorLoader constructor code would look like this:

return new CursorLoader(getActivity(), YourContentProvider.CONTENT_URI,
            FAVORITES_SUMMARY_PROJECTION, null, null, null);

For completeness, you should change the variable names to be more descriptive, so rather than TUTORIALS_BASE_PATH and CONTENT_URI, you should change it to something like LOCATIONS_BASE_PATH and LOCATIONS_URI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文