【问题标题】:Contacts contentProvider is returning an empty cursor联系人 contentProvider 正在返回一个空游标
【发布时间】:2012-03-15 10:28:11
【问题描述】:

我想显示联系人的电话号码。如果我在模拟器中运行我的代码没有任何反应,但是当我在我的智能手机中运行它时,我在 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);
       }
   } 

请问有什么解决办法吗?

【问题讨论】:

  • 您的查询没有任何结果,因此它返回一个空光标给您。您的异常发生是因为您试图从不存在的光标中提取信息。我对 Contacts API 不够熟悉,无法确定为什么要返回空光标。

标签: android listview checkbox contactscontract


【解决方案1】:

几乎可以肯定的是,您的联系人数据库中有一些条目没有电话号码。您的代码似乎没有考虑到这一点。在访问 numberCursor 之前检查 numberCursor.moveToFirst() 的返回值

【讨论】:

    【解决方案2】:

    如果您想使用 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

    注意:代码只会返回联系人的第一个电话号码

    【讨论】:

      猜你喜欢
      • 2012-11-29
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多