android - 内容提供者 - 多个 where 参数

标签 android android-contentprovider android-contentresolver

我是内容提供商的新手,我一直在引用 this文档以便理解和创建自定义内容提供程序。

我在内容提供者的内容描述符类中有这样的路径:

public static final String PATH = "tbl_reco_index_contents";
public static final String PATH_FOR_ID = "tbl_reco_index_contents/*";

使用下面的代码,我可以毫无问题地从我需要的列中获取数据:

    public static final String AUTHORITY = "com.nyk.launcherprovider";
private static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY);
public static final String PATH = "tbl_reco_index_contents";
public static final Uri CONTENT_URI = BASE_URI.buildUpon().appendPath(PATH).build();
    cur = this.getContentResolver().query(CONTENT_URI, new String[]{
            "reco_index_content_name",
            "reco_index_content_url"
        }, null, null, null);

    cur.moveToFirst();
    for(int i=0;i<cur.getCount();i++){
        System.out.println("Name is:"+cur.getString(10));
        System.out.println("URL is:"+cur.getString(11));
        cur.moveToNext();
    }

我不知道如何在这里使用 where 条件获取数据。 IE;如果我需要添加像 WHERE user_profile_number = 2 and pkg_name = 'abc' 这样的条件,我该如何处理它以及上面的代码。

非常感谢任何帮助。

最佳答案

当您使用波纹管语句时,它会调用内容提供程序中的PATH

Cursor cursor = getContentResolver().query(YOUR_URI, null, "user_profile_number = ? AND pkg_name = ? ", new String[]{"2", "abc"}, null);

如果你想根据id调用数据就意味着你想调用PATH_FOR_ID 然后使用

Cursor cursor = getContentResolver().query(ContentUris.withAppendedId(CONTENT_URI, id), null, null, null, null);

关于android - 内容提供者 - 多个 where 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16918397/

相关文章:

android - 估计android中的剩余电池时间

android - 是否有一种 "proper"方法可以将内容提供程序实现与其用户分开?

java - android中将异步方法转为同步方法

android - 我无法从 FFMPEG 为 android 构建 .so 文件

android - 哪个线程运行 ContentProvider?

android - 根据电话号码添加原始联系人时联系人重复

Android 获取所有联系人列表(姓名、电子邮件、电话)需要超过一分钟的时间来获取大约 700 个联系人

android - 我是否必须通过 ContentResolver 关闭使用过的游标?

android - 将事件添加到 Android 日历失败

android - EditText 上的 SetError