0

I have a list of row ids and i'm trying to query them using contentprovider. This is the method and parameters:

   Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String      sortOrder)

String selection is what determines the ID of the row. I have a list of row IDs and want to query the list. How would I implement this?

EDIT:

If anyone was interested, I finally solved this. I was able to achieve this by using a text filter and onTextChangedListener

   EditText itemName = (EditText) v.findViewById(R.id.inputSearch);
    itemName.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String wildcardQuery = "%" + s + "%";
            Cursor cur = getActivity().getContentResolver().query(
                    DrugContentProvider.CONTENT_URI,
                    null,
                    DataBaseHelper.COLUMN1 + " LIKE ?",
                    new String[]{wildcardQuery},
                    DataBaseHelper.COLUMN2 + " ASC");


            testadapter = new TestPackageAdapter(getActivity().getBaseContext(), cur);

            testadapter.getFilter().filter(s.toString());
            mList.setAdapter(testadapter);
        }
Minh Tran
  • 783
  • 1
  • 9
  • 20

1 Answers1

1

You'll need to create the value list (?,?,?...) see here Sqlite Query for multiple values in one column It should work the same in the content provider since most just pass through to a sqlite database, and you didn't mention what content provider you are using.

Similar to

cursor = database.query(contentUri, projection, "columname IN(?,?)", new String[]{"value1" , "value2"}, sortOrder);
Community
  • 1
  • 1
Eric Woodruff
  • 6,380
  • 3
  • 36
  • 33
  • i'm using android.content.ContentProvider. What if the number of row changes. How would I implement that with the IN operator. It look like it is predefined to two IDs. – Minh Tran Jan 22 '14 at 01:58
  • You need to use a StringBuilder or somesuch and create the IN(...) list. – Eric Woodruff Jan 22 '14 at 03:43
  • I tried implementing it using string builder but I get java.lang.RuntimeException: Caused by: android.database.sqlite.SQLiteException: near "IN": syntax error (code 1): , while compiling: SELECT _id, col_01 FROM table_01 WHERE (_id = IN (150,120)) ORDER BY col_01 ASC – Minh Tran Jan 22 '14 at 07:19
  • You probably need to use quotes like lastname IN ('Hernandez', 'Jones', 'Roberts', 'Ruiz' but in your case you should follow my example and use ? parameters and the string array of values. – Eric Woodruff Jan 22 '14 at 07:34
  • I gave up using that approach and went with a relational database. I created a new table to hold the ids of the table i'm trying to query. Here is the example I used, http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/. – Minh Tran Jan 23 '14 at 05:55