カーソルを使用して検索クエリを作成しようとしています。常にカーソルを空として返します。このクエリは、検索しているテキストのアイテムのリストを返すことになっています(String[] selectionArgs = { inputText };)。ここで、inputText は検索している検索語です。
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.Contacts._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ? ";
String[] selectionArgs = { inputText };
// Expecting problem in the query.
Cursor people = getContentResolver().query(uri, projection, selection, selectionArgs, null);
int indexName = people.getColumnIndex(StructuredName.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int idIdx = people.getColumnIndexOrThrow(PhoneLookup._ID);
if(!people.moveToFirst())
{
Log.w("No Cursor.., ","No cursor.., Cursor is empty..");
}
do {
String id = people.getString(idIdx);
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
return people;
以下のエラーを返します。
W/No Cursor.., ( 1303): No cursor.., Cursor is empty..
W/Filter ( 1303): An exception occured during performFiltering()!
W/Filter ( 1303): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
W/Filter ( 1303): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
W/Filter ( 1303): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
W/Filter ( 1303): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
W/Filter ( 1303): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
W/Filter ( 1303): at rebornlabs.sms2india.sms.app.ContactActivity.getSearchedContacts(ContactActivity.java:179)
W/Filter ( 1303): at rebornlabs.sms2india.sms.app.ContactActivity$3.runQuery(ContactActivity.java:102)
W/Filter ( 1303): at android.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:309)
W/Filter ( 1303): at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
W/Filter ( 1303): at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
W/Filter ( 1303): at android.os.Handler.dispatchMessage(Handler.java:99)
W/Filter ( 1303): at android.os.Looper.loop(Looper.java:123)
W/Filter ( 1303): at android.os.HandlerThread.run(HandlerThread.java:60)
クエリに問題があると予想しています..何を見逃したのかわかりません。
Cursor people = getContentResolver().query(uri, projection, selection, selectionArgs, null);