user264953
user264953

Reputation: 1967

Content provider - multiple where parameters

I am a newbie with content providers and I have been referring to this document in order to understand and create a custom content provider.

I have paths like this in the content descriptor class for content provider:

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

With the below code, I am able to fetch the data from the columns which I need, without any issues:

    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();
    }

I do not know, how I can fetch data using a where condition here. ie; if I need to add a condition like WHERE user_profile_number = 2 and pkg_name = 'abc' , how do I handle that along with the code above.

Any help is much appreciated.

Upvotes: 3

Views: 5576

Answers (2)

Sumit Bhatt
Sumit Bhatt

Reputation: 718

When you use bellow statement then it call PATH in the content provider

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

If you want to call data based on id it means you want to call PATH_FOR_ID then use

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

Upvotes: 8

Aditya
Aditya

Reputation: 5619

Use

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);

the function used here is

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

Upvotes: 0

Related Questions