0

I want to display the phone numbers of the contacts. If I run my code in the emulator nothing happens, but when i run it in my SmartPhone I found this error in the logcat ---->

  android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

the code i m working with is

  Cursor peopleCursor =  
   getContentResolver().query(ContactsContract.Contacts.CONTENT_URI
    ,null, null,null, null);
   String [] nb = new String[peopleCursor.getCount()];
     String [] Tname = new String[peopleCursor.getCount()];
      if(peopleCursor.getCount()>0){
       peopleCursor.moveToFirst();
        Cursor numberCursor;
          for(int i=0;i<peopleCursor.getCount();i++)
         {
        //get number
     numberCursor=
     getContentResolver().query(ContactsContract.  
         CommonDataKinds.Phone.CONTENT_URI, new String[]   
        {ContactsContract.CommonDataKinds.Phone.NUMBER} 
   ,ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor 
   .getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID))    
      , null,null);
     numberCursor.moveToFirst();
   String number=numberCursor.getString(numberCursor
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
     nb[i]=number;

    //get name
   String name=peopleCursor.getString(numberCursor
          .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
       Tname[i]=name;
      peopleCursor.moveToNext();
      }
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,  
       R.layout.contact_entry, peopleCursor,nb
                      , new int[] {R.id.checkBox});
        lv.setAdapter(adapter);
       }
   } 

any solutions please?

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
Wael Ilahi
  • 41
  • 3
  • Your query doesn't have any results, so it is returning an empty cursor to you. Your exception happens because you are trying to pull information out of the cursor that isn't there. Im not familiar enough with the Contacts API to know for sure why you are getting the empty cursor back. – FoamyGuy Feb 25 '12 at 00:32

2 Answers2

0

If you want to work with a SimpleCursorAdapter I can not help you a lot since I am new to this too, but the following Code should give you the correct phonenumbers.

Porbably not the exact Answer you are looking for, but it will give you the Phone number for each Contact if the contact has one:

    //Initialize Adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1);

    //Get List of all COntacts
    Uri uri = ContactsContract.Data.CONTENT_URI;

    String columns[] = {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
    };

    Cursor contactCursor = mContext.getContentResolver().query(
            uri,
            columns,
            null,
            null,
            null
    );

    //For every Rsult do this
    while (contactCursor.moveToNext()) {
        String phoneNO = "";
        String name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String id = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

        //Check if COntact has a phone number
        if (Integer.parseInt(contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){

            //Create Cursor to get Phone Number (You need the Phone.Contact_ID here)
            Cursor numberCursor = mContext.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[]{id},
                    null,
                    null
            );
            if (numberCursor.moveToFirst()){
                phoneNO = numberCursor.getString(numberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));    
            }

        }
        //Create String you want to show in List
        String contactList = "name: " + name + " Phonenumber: " + phoneNO;
        adapter.add(contactList);
    }
    listView.setAdapter(adapter);

Maybe this answer can help you: How to get contacts' phone number in Android

Be careful: the code will only return you the first phonenumber there is for a contact

Taco
  • 1
0

Almost certainly the issue is that you have some entries in your contact database without phone numbers. Your code doesn't appear to take that into account. check the return value of numberCursor.moveToFirst() before accessing numberCursor

Philip Pearl
  • 1,523
  • 16
  • 26