mardi 17 mars 2015

Funny(Glitchy) behaviour querying content resolver

I was trying a small experiment with androids telephony content provider so I wrote this code in my main activity to get 40 rows in descending order by _id and then use the last rows id to get 20 more rows in another cursor having _id < lastRowId



Uri uri = Uri.parse("content://mms-sms/conversations");
Cursor cursor = getContentResolver().query(uri, new String[]{"_id", "ct_t"}, null, null,
"_id desc limit 40");
Log.d(TAG, "cursor: " + String.valueOf(cursor.getCount()));

cursor.moveToLast();

String lastId = String.valueOf(cursor.getInt(0));

Log.d(TAG, "Last cursor id: " + lastId);

cursor.close();

Cursor cursor2 = getContentResolver().query(uri, new String[]{"_id", "ct_t"}, "_id < ?", new String[] {lastId},
"_id desc limit 20");
Log.d(TAG, "cursor2: " + String.valueOf(cursor2.getCount()));

cursor2.moveToLast();

lastId = String.valueOf(cursor2.getInt(0));

Log.d(TAG, "Last cursor2 id: " + lastId);

cursor2.close();


which leads to 0 rows being returned for cursor2 and a crash while trying to read its 0th column (IndexOutOfBounds) but the funny part is that by replacing



Cursor cursor2 = getContentResolver().query(uri, new String[]{"_id", "ct_t"}, "_id < ?", new String[] {lastId},
"_id desc limit 20");


with



Cursor cursor2 = getContentResolver().query(uri, new String[]{"_id", "ct_t"}, "_id < 653", null,
"_id desc limit 20");


where 653 is stored in lastId, I get a valid cursor with a count of 20


Can anyone help me out in figuring what the problem might be?


Aucun commentaire:

Enregistrer un commentaire