Contacts contentProvider正在返回一个空光标



我想显示联系人的电话号码。如果我在模拟器中运行代码,什么都不会发生,但当我在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);
       }
   } 

有什么解决方案吗?

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

如果你想使用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);

也许这个答案可以帮助你:如何获取联系人';安卓中的电话号码

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

相关内容

  • 没有找到相关文章

最新更新