Search code examples
androidsqliteandroid-sqliteandroid-cursorloaderandroid-loader

Loader<Cursor> load only selected row + next row


In activity A I am loading a list with all the values from my table and have set a setOnItemClickListener to start activity B and send an uri with the selected item's id[1] via send data

[1] Uri currentTestUri = ContentUris.withAppendedId(TestEntry.CONTENT_URI, id);

In activity B I have my onCreateLoader with the projection:

String[] projection = {
       TestEntry._ID,
       TestEntry.COLUMN_TEST_ONE,
       TestEntry.COLUMN_TEST_TWO}

...with the return statement

return new CursorLoader(this,  
    mCurrentTestUri, //Got this from Activity A         
    projection,             
    null,                   
    null,                   
    null);  

And my onLoadFinished looks something like this:

if (cursor.moveToFirst()) {
      int oneColumnIndex = cursor.getColumnIndex(TestEntry.COLUMN_TEST_ONE);
      int twoColumnIndex = cursor.getColumnIndex(TestEntry.COLUMN_TEST_TWO);

      String currentOne = cursor.getString(oneColumnIndex);
      String currentTwo = cursor.getString(twoColumnIndex);

      textViewOne.setText(currentOne);
      textViewTwo.setText(currentTwo);
}

So far so good, now I wish to show values from the next row (right beneath it) but with a different projection (I only need the _ID and COLUMN_TEST_ONE) and have onLoadFinished display COLUMN_TEST_ONE's value in textViewThree.

[values from both rows should be shown at the same time, not one or another]

I can get the ID of the next row from activity A with [2] and send it as a string via putExtra but that's about all I have so far.

[2]

String nextItemId = String.valueOf(listView.getItemIdAtPosition(position + 1));
if((position+1) < lListView.getCount()) {
    intent.putExtra("prevID", nextItemId);
}

..or I could create a valid URI path with the next row ID and send it as a string from Activity A and convert it to a URI in activity B if needed with:

ContentUris.withAppendedId(TestEntry.CONTENT_URI, nextItemId)

How should I change my activity B to load values from the next row and the current one onCreate?


Solution

  • The problem is with your query:

    
        Uri currentTestUri = ContentUris.withAppendedId(TestEntry.CONTENT_URI, id);
    
    

    Here you are specifying, that you want to query only rows, that have specific id. Any row, which has different id won't be returned in the Cursor.

    Instead, query the table with appropriate selection arguments:

    
        // Load all rows that have id `firstId` or `secondId`
        return new CursorLoader(this,  
            TestEntry.CONTENT_URI,
            projection,             
            TestEntry._ID + "=? OR " + TestEntry._ID + "=?",                   
            new String[] {firstId, secondId},                   
            null);
    
    

    Then you can get the value of secondId row following way:

    
        if (cursor.moveToFirst()) {
              ...
    
              textViewOne.setText(currentOne);
              textViewTwo.setText(currentTwo);
    
              if (cursor.moveToNext()) {
                  int index = cursor.getColumnIndex(TestEntry.COLUMN_TEST_ONE);
                  String next = cursor.getString(index);
                  // Use `next` as needed, may be passed to next activity via extras
              }
        }