Lampione
Lampione

Reputation: 1608

Empty cursor row when filtering with selection

I'm not confident with cursors and I'm facing some problem when filtering one with a WHERE clause.

What I'm doing:

ContentResolver contentResolver = context.getContentResolver();
    Uri uriConversation = Uri.parse("content://mms-sms/conversations/");

    String[] projection = new String[]{"*"};
    String selection = "address=" + phoneNumberForThread;

    Cursor cursor = contentResolver.query(uriConversation, projection, null, null, null);

Executing this code the cursor get filled and works perfectly. However, if I swap the null selection argument with my selection String as

Cursor cursor = contentResolver.query(uriConversation, projection, selection, null, null);

I then get an empty cursor. I even check for !phoneNumberForThread.isEmpty()

I think I'm doing something wrong but again I am not confident with cursor yet. Any help would be really appreciated.

Upvotes: 1

Views: 49

Answers (1)

Marcus
Marcus

Reputation: 6717

If your input variabel is stored as a string data type, you should enclose it with ' as such:

String selection = "address='" + phoneNumberForThread + "'";

Upvotes: 2

Related Questions