2014-01-30 6 views
0

총 연락처 수는 있지만 문제는 그 전화 번호부의 연락처 수가 전화 번호부와 다르다는 것입니다. 나는 데이터베이스에서 얻고있다. 는 여기에 내 코드입니다 :전화 번호부의 총 연락처 수를 텍스트보기로 표시하는 방법은 무엇입니까

Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 
     // Or this cursor 
     Cursor cursor = managedQuery(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
       null, null); 

     int count = cursor.getCount(); 

     try { 
      tv1.setText(String.valueOf(count)); 
      Log.i("Contacts: ", String.valueOf(count)); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

나는 전체 데이터베이스에 연락처 수 있지만 전화 번호부에 표시된 연락처에서의 다른를 얻을 수 있어요.

모든 제안이나 도움은 정말 감사하겠습니다.

+0

연락처 앱에서 가입 한 주소록 수를 계산하지 않기 때문에 올바른 정보가 표시 될 수 있습니다. –

+0

정보를 제공해 주셔서 감사합니다. 거기에 정확한 정보를 얻을 수있는 다른 방법이 있습니까? – mike20132013

+0

필자가 지나가고있는 4 개의 null 매개 변수와 관련이 있습니다. [this] (http://stackoverflow.com/questions/1721279/how-to-read-contacts-on-android-2-0) 링크에서 다른 방법을 시도해보십시오. –

답변

1

나는 여전히 배우는 중이므로 이보다 더 빠른 방법이 있다고 생각하지만, 고유 한 이름 만 계산할 때는 if 문을 사용하여 고유 한 이름을 사용하여 내 연락처 목록에 할당하고 숫자를 계산합니다. 독특한 이름으로되어있어서 정확한 번호를 알 수있었습니다. 여기에 제가 사용하는 코드가 있습니다. 누군가가 가지고 가서 더 효율적인 방법을 조언 해주기를 바랍니다. 희망이 도움이됩니다.

"nameCount"는이 예에서 찾고있는 번호입니다.

private void getContacts() { 

     String name = ""; 
     String contact_id; 
     int nameCount = 0; 

     uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     projection = new String[] { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID }; 
     selection = null; 
     selectionArgs = null; 
     sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME; 

     Cursor peopleCursor = getContentResolver().query(uri, projection, 
       selection, selectionArgs, sortOrder); 

     if (peopleCursor != null) { 

      int indexName = peopleCursor 
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
      int indexId = peopleCursor 
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID); 

      while (peopleCursor.moveToNext()) { 

       // this is a separate activity used for my listView. 
       //It containts 2 strings (name and _id) with getters and setters for both 
       ContactNameItems nameItems = new ContactNameItems(); 

       // Filter out no-name contacts such as auto-added email addresses from gmail 
       // Compare to value of 'name' to see if they're the same, if so then pass 
       if (!peopleCursor.getString(indexName).isEmpty() && 
          !peopleCursor.getString(indexName) 
            .equalsIgnoreCase(name) { 

        name = peopleCursor.getString(indexName); 
        contact_id = peopleCursor.getString(indexId); 
        nameCount++; //Do something with this 


        nameItems.setName(name); 
        nameItems.set_id(contact_id); 

        //Listview to add my 'nameItems' 
        mListView.add(nameItems); 

        mListAdapter.notifyDataSetChanged(); 
       } 
      } 
      peopleCursor.close(); 
     } 
    } 
관련 문제