2

我是内容提供商的新手,我一直在参考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条件来获取数据。即;如果我需要添加如WHERE user_profile_number = 2 and pkg_name = 'abc'这样的条件,我该如何处理以上代码。

任何帮助,非常感谢。

回答

0

使用

cur = this.getContentResolver().query(CONTENT_URI, new String[]{ 
     "reco_index_content_name", 
     "reco_index_content_url" 
    }, "user_profile_number = 2 and pkg_name = 'abc'", null, null, null, null); 

这里使用的功能是

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

+1

您还可以使用选择的参数,如果你不知道什么会在where子句中的值的数据。 – anon

4

当您使用波纹管的语句,然后调用它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);