2012-02-24 2 views
0

연락처의 전화 번호를 표시하고 싶습니다. 나는 아무 일도 발생하지 에뮬레이터에서 내 코드를 실행,하지만 난 내 스마트 폰에서 실행할 때이연락처 contentProvider가 빈 커서를 반환합니다.

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); 
     } 
    } 
입니다

어떤 해결책을주십시오?

+0

검색어에 아무런 결과가 없으므로 빈 커서가 사용자에게 반환됩니다. 커서가없는 곳에서 정보를 가져 오기 때문에 예외가 발생합니다. 빈 API가 왜 돌아 오는지 알기 위해 연락처 API에 익숙하지 않습니다. – FoamyGuy

답변

0

거의 확실하게 문제는 전화 번호없이 연락처 데이터베이스에 몇 가지 항목이 있다는 것입니다. 코드가이를 고려하지 않습니다. 숫자에 액세스하기 전에 numberCursor.moveToFirst()의 반환 값을 확인하십시오.

0

SimpleCursorAdapter를 사용하고 싶다면 다음과 같은 코드가 올바른 폰 번호를 제공해야하므로 많은 도움을받을 수 없습니다.

Porbably

하지 당신이 찾고있는 정확한 대답하지만, 접촉 한 경우 그것은 당신에게 각 연락처의 전화 번호를 줄 것이다 :

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

주의 : 코드는 연락처의 첫 번째 전화 번호 만 반환합니다.

관련 문제