2012-12-12 87 views
0

我正在嘗試使用光標編寫搜索查詢。它總是將光標返回爲空。 這個查詢應該返回我正在搜索的文本的項目列表(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); 
+2

這只是表示您的聯繫人中沒有與「inputText」匹配的_exact_匹配項。 – Sam

回答

1

如果你想要做的基於部分結果的搜索(如搜索「他」會給結果「你好」,「男子漢」,「重」,「特他自己」。等你應使用代替LIKE =?語句

前。

String[] projection = new String[] { 
    ContactsContract.Contacts._ID, 
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
    ContactsContract.CommonDataKinds.Phone.NUMBER 
}; 
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like '%" + inputText + "%'"; 

Cursor people = getContentResolver().query(uri, projection, selection, null, null); 

像語句不玩好與selectionArgs兩個這就是爲什麼我通過在空爲他們..

此外,您需要改變你的代碼流。如果你的cu rsor不包含條目,你還在試圖檢索光標值..嘗試這樣的事情..

while(people.moveToNext()) { 
     String id = people.getString(idIdx); 
     String name = people.getString(indexName); 
     String number = people.getString(indexNumber); 
     // Do work... 
    } 

您可以移除if (!people.moveToFirst())聲明,因爲它有效地做什麼,但打印日誌語句。 while語句將執行相同的檢查,並且只將數據分配給列表(如果存在)

+0

但在應用查詢後拋出錯誤。 – swastican

+0

@PrakashP什麼是錯誤? – dymmeh

+0

感謝它現在工作正常 – swastican