联系人contentProvider返回一个空光标

时间:2012-02-24 23:59:25

标签: android listview checkbox contactscontract

我想显示联系人的电话号码。如果我在模拟器中运行我的代码没有任何反应,但是当我在我的SmartPhone中运行它时,我在logcat中发现了这个错误---->

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

我正在使用的代码是

  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);
       }
   } 

请问任何解决方案?

2 个答案:

答案 0 :(得分:0)

几乎可以肯定的是,您的联系人数据库中有一些没有电话号码的条目。您的代码似乎没有考虑到这一点。在访问numberCursor

之前检查numberCursor.moveToFirst()的返回值

答案 1 :(得分:0)

如果你想使用SimpleCursorAdapter,我也不能帮助你,因为我也是新手,但是下面的代码应该给你正确的语音。

可能不是您正在寻找的确切答案,但如果联系人有一个,它会为您提供每个联系人的电话号码:

    //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);

也许这个答案可以帮到你: How to get contacts' phone number in Android

小心:代码只返回给你一个联系人的第一个phonenumber