Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:55:19+00:00 2026-06-15T21:55:19+00:00

I am trying to write a search query using cursor. It always returns cursor

  • 0

I am trying to write a search query using cursor. It always returns cursor as empty.
This query is supposed to return list of items for the text i am searching(String[] selectionArgs = { inputText };).
where the inputText is search term which i am searching.

        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;

It returns following error.

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)

I am expecting some issue in the query.. Dont know what i have missed.

Cursor people = getContentResolver().query(uri, projection, selection, selectionArgs, null);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:55:21+00:00Added an answer on June 15, 2026 at 9:55 pm

    If you want to do a search based on partial results (as in searching “He” will give results “Hello”, “He-man”, “Heavy”, “Te He he”. etc you should use LIKE instead of a = ? statement

    ex.

    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 statements don’t play nice with selectionArgs which is why I pass in null for them..

    Additionally, you need to change your code flow. If your cursor doesn’t contain entries, you are still trying to retrieve values from the cursor.. Try something like this..

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

    You can remove your if (!people.moveToFirst()) statement since it’s effectively doing nothing but printing a log statement. The while statement will perform the same check and only assign data to the list if it exists

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to write a complex search function using the LIKE query
I am trying to write a query that will search a list of strings
I am trying to write a query to search a subclass attribute that is
I'm trying to write an SQL query that would search within a CSV (or
I'm trying to write a query for the find-as-you-type search bar. What I want
I'm pretty new to MYSQL. I'm trying to write a query which will search
I'm trying to write a free text search algorithm for finding specific posts on
I am trying to write a query that will search for orders that contain
I'm trying to write a LEPL grammar to describe a Boolean search language. This
So I'm trying to optimize a query that does a text search on 1000s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.