0
votes

I have a problem with my Content Provider. I use the content provider in one of my Activities with a CursorLoader and an adapter to show a list.

The list shows the contents of the Task table if and only if i do NOT specify a selection and selectionArgs parameters in the CursorLoader constructor.

I have tried passing in nulls for the selection and selection args and this returns the contents of the whole table. What i am trying to do is select the full projection of the table based on the C_TASK_CALLID column and passing in a callID.

So what i want to do is return a cursor base on some sql like this:

select * from table TASK where C_TASK_CALLID = callID;

Can anyone tell me why i am getting the folloing error?

I have looked at the following SO post, but it indicates there is a mis-match between the selection and selectionArgs. In my case, this is not true as i am passing in only one param.

link

.

I get the following error:

06-01 14:05:56.631 32037-32037/com.carefreegroup.rr3 E/ViewCompletedTasksActivity: about to create CursorLoader.....LoginValidate.C_TASK_CALLID = taskcallid callID =  d5f5482f-b240-4eb6-8be1-489a8d75af9b
06-01 14:05:56.636 32037-2691/com.carefreegroup.rr3 E/RR3ContentProvider: inside RR3ContentProvider query method
06-01 14:05:56.636 32037-2691/com.carefreegroup.rr3 E/LoginValidate: NfcScannerApplication.getSecretKey() = 12345
06-01 14:05:56.636 32037-2691/com.carefreegroup.rr3 E/RR3ContentProvider: CASE TASKS
06-01 14:05:56.636 32037-2691/com.carefreegroup.rr3 E/RR3ContentProvider: About to do the query method in content provider
06-01 14:05:56.645 32037-2691/com.carefreegroup.rr3 E/CustomExceptionHandler: stack = java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                  at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                  at java.lang.Thread.run(Thread.java:762)
                                                                               Caused by: net.sqlcipher.database.SQLiteException: bind or column index out of range: handle 0x7bf058da88
                                                                                  at net.sqlcipher.database.SQLiteProgram.native_bind_string(Native Method)
                                                                                  at net.sqlcipher.database.SQLiteProgram.bindString(SQLiteProgram.java:245)
                                                                                  at net.sqlcipher.database.SQLiteQuery.bindString(SQLiteQuery.java:183)
                                                                                  at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
                                                                                  at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1820)
                                                                                  at net.sqlcipher.database.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
                                                                                  at net.sqlcipher.database.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
                                                                                  at com.carefreegroup.rr3.RR3ContentProvider.query(RR3ContentProvider.java:396)
                                                                                  at com.carefreegroup.rr3.RR3ContentProvider.query(RR3ContentProvider.java:23)
                                                                                  at android.content.ContentProvider.query(ContentProvider.java:1027)
                                                                                  at android.content.ContentProvider$Transport.query(ContentProvider.java:243)
                                                                                  at android.content.ContentResolver.query(ContentResolver.java:536)
                                                                                  at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
                                                                                  at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
                                                                                  at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                                                                                  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                                                                                  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
                                                                                  at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                  at java.lang.Thread.run(Thread.java:762) 

.

Below is my Task table:

public static final String C_ID_TASK_QUEST_COMM = BaseColumns._ID;
    public static final String C_TASK_QUEST_COMM_QUESTSION = "taskquestcommquestion";
    public static final String C_TASK_QUEST_COMM_COMMENTS = "taskquestcommcomments";
    public static final String C_TASK_QUEST_ID = "taskquestid";
    public static final String C_TASK_QUEST_TYPE = "taskquesttype";
    public static final String C_TASK_CALLID = "taskcallid";
    public static final String C_TASK_SENT_TO_SERVER = "tasksenttoserver";
    public static final String C_TASK_VALUE = "taskvalue";

.

below is my CursorLoader constructor in my Activity:

@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        Log.e(TAG, "inside3 onCreateLoader in ViewCompletedTasksActivityAsync");




        String[] projection = { LoginValidate.C_ID_TASK_QUEST_COMM, LoginValidate.C_TASK_QUEST_COMM_QUESTSION, LoginValidate.C_TASK_QUEST_COMM_COMMENTS,
                LoginValidate.C_TASK_QUEST_ID,
                LoginValidate.C_TASK_QUEST_TYPE , LoginValidate.C_TASK_CALLID, LoginValidate.C_TASK_SENT_TO_SERVER, LoginValidate.C_TASK_VALUE};



        String [] selectionArgs = {callID};


        Log.e(TAG, "about to create CursorLoader.....LoginValidate.C_TASK_CALLID = " + LoginValidate.C_TASK_CALLID + " callID = " + callID);

        cursorLoader = new CursorLoader(this, RR3ContentProvider.CONTENT_URI_TASKS, projection, LoginValidate.C_TASK_CALLID, selectionArgs , null);

        return cursorLoader;
    }

.

Below is my contentProvider:

public class RR3ContentProvider extends ContentProvider {

   private static final String TAG = RR3ContentProvider.class.getSimpleName();
   NfcScannerApplication nfcAppObj; 

   static final String PROVIDER_NAME = "com.xxxxx.xxx.ContentProvider";





    static final String URLTASKS = "content://" + PROVIDER_NAME + "/tasks";
    static final Uri CONTENT_URI_TASKS = Uri.parse(URLTASKS);


    static final String _ID_TASKS = "_id";
    static final String TASK_QUESTION = "taskquestcommquestion";


    static final String TABLETASKS = "tabletaskquestion";

    private static HashMap<String, String> TASK_PROJECTION_MAP;

    static final int TASKS = 9;
    static final int TASK_ID = 10;





   static final UriMatcher uriMatcher;
   static{
      uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

       uriMatcher.addURI(PROVIDER_NAME, "tasks", TASKS);
       uriMatcher.addURI(PROVIDER_NAME, "tasks/#", TASK_ID);
   }

   /**
    * Database specific constant declarations
    */
   private SQLiteDatabase db;


   @Override
   public boolean onCreate() {


       Context applicationContext = getContext().getApplicationContext();
       nfcAppObj = getRealApplication(applicationContext);



      Log.e(TAG, "inside RR3ContentProvider onCreate");
      return (nfcAppObj == null)? false:true;
   }



   @Override
   public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

       Log.e(TAG, "inside RR3ContentProvider query method");


       db = nfcAppObj.getDb();

      SQLiteQueryBuilder qb = new SQLiteQueryBuilder();


      switch (uriMatcher.match(uri)) {








          case TASKS:
              Log.e(TAG, "CASE TASKS");
              qb.setTables(TABLETASKS);
              qb.setProjectionMap(TASK_PROJECTION_MAP);



              /*if (sortOrder == null || sortOrder == ""){

                  sortOrder = LOG_CREATED_TIME + " DESC";
              }*/

              break;

          case TASK_ID:
              Log.e(TAG, "CASE TASK_ID");
              qb.setTables(TABLETASKS);
              qb.appendWhere( _ID_TASKS + "=" + uri.getPathSegments().get(1));

              /*if (sortOrder == null || sortOrder == ""){

                  sortOrder = LOG_CREATED_TIME + " DESC";
              }*/

              break;

      default:

         throw new IllegalArgumentException("Unknown URI " + uri);
      }




      Log.e(TAG, "About to do the query method in content provider");

      Cursor c = qb.query(db,   projection, selection, selectionArgs,
                          null, null, sortOrder);
      /** 
       * register to watch a content URI for changes
       */
      c.setNotificationUri(getContext().getContentResolver(), uri);

      return c;

   }//end of query












   @Override
   public String getType(Uri uri) {
      switch (uriMatcher.match(uri)){
      /**
       * Get all  records 
       */


          case TASKS:
              return "vnd.android.cursor.dir/vnd.example.tasks";
          /**
           * Get a particular record
           */
          case TASK_ID:
              return "vnd.android.cursor.item/vnd.example.tasks";
      default:
         throw new IllegalArgumentException("Unsupported URI: " + uri);
      }
   }
}

.

1
your query have incorect WHERE statment - you are passsing selectionArgs but there is no place to bind it - Selvin
@Selvin i found the solution here. stackoverflow.com/questions/25754360/… I thought the CursorLoader constuctor abstracted everything away, so i only had to pass in the selection and selectionArgs. but it turns out that i have to do the following and supply the placeholders. cursorLoader = new CursorLoader(this, RR3ContentProvider.CONTENT_URI_TASKS, projection, LoginValidate.C_TASK_CALLID + " = ?", new String[]{callID.trim()} , null); thanks anyway - turtleboy
yeah ... you shoyld pass LoginValidate.C_TASK_CALLID + "=?" there - Selvin

1 Answers

0
votes

Your sql query error! "select * from table TASK where C_TASK_CALLID = callID;" redundant "table". Change "select * from TASK where C_TASK_CALLID = callID;"