首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现搜索查询时,游标始终返回为空

问实现搜索查询时,游标始终返回为空
EN

Stack Overflow用户
提问于 2012-12-13 01:45:26
回答 1查看 1.3K关注 0票数 0

我正在尝试使用游标编写一个搜索查询。它总是将游标返回为空。此查询应该返回我正在搜索的文本的项目列表(String[] selectionArgs ={ inputText };)。其中inputText是我正在搜索的搜索词。

代码语言:javascript
复制
        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;

它返回以下错误。

代码语言:javascript
复制
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)

我希望在查询中出现一些问题。不知道我错过了什么。

代码语言:javascript
复制
Cursor people = getContentResolver().query(uri, projection, selection, selectionArgs, null);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-13 03:21:11

如果您想根据部分结果进行搜索(如在搜索" He“时),则会给出结果"Hello”、"He-man“、"Heavy”、"Te he he“。等您应该使用LIKE而不是a=?语句

例如。

代码语言:javascript
复制
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);

Like语句不能很好地处理selectionArgs,这就是为什么我为它们传入null的原因。

此外,您还需要更改代码流。如果游标不包含条目,则仍在尝试从游标中检索值。试试这样的东西..

代码语言:javascript
复制
while(people.moveToNext()) {
        String id = people.getString(idIdx);
        String name   = people.getString(indexName);
        String number = people.getString(indexNumber);
        // Do work...
    }

您可以删除if (!people.moveToFirst())语句,因为它实际上只打印一条日志语句。while语句将执行相同的检查,并且仅将数据分配给列表(如果存在

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13845575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档