Android:联系人列表有重复的名称

时间:2015-12-08 07:23:26

标签: android android-contentprovider android-contacts android-contentresolver

我在排序顺序中有一个联系人列表。但在我的联系人列表中,该名称与相同的号码重复。我认为问题是由于联系人列表与不同帐户同步。

我查看哈希地图。但是当我使用哈希映射时,结果没有按名称排序。

private static final String[] PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Phone.NUMBER
};

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
  null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");

if (cursor != null) {
    try {
        int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        String nameContact = cursor.getString(nameIndex);
    finally {
        cursor.close();
    }
}

适配器

holder.name.setText(itemListPogo.get(position).getItemName());

任何人都可以帮助避免重复名称。

4 个答案:

答案 0 :(得分:2)

我会建议你使用我的搜索结束的地方,它会给你最快的结果。

public static List<ContactDTO> getPhone(Context context) {
    List<ContactDTO> contactList = new ArrayList<ContactDTO>();
    ContentResolver cr = context.getContentResolver(); 
    String[] PROJECTION = new String[] { 
        ContactsContract.RawContacts._ID, 
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + Phone.TYPE +"=" + Phone.TYPE_MOBILE;     
    String order = ContactsContract.Contacts.DISPLAY_NAME + " ASC";// LIMIT " + limit + " offset " + lastId + "";

    Cursor phoneCur = cr.query(uri, PROJECTION, filter, null, order);
    while(phoneCur.moveToNext()) {
        ContactDTO dto = new ContactDTO();
        dto.setName("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        dto.setMobileNo("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        dto.setPhotoUrl("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
        dto.setContactId("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)));
        contactList.add(dto);
    }
    phoneCur.close();

    return contactList;
} 

其中ContactDTO是Simple POJO class。

答案 1 :(得分:1)

您看到重复的联系人,因为他们属于不同的帐户。即如果与Facebook,WhatsApp和Google帐户同步,则相同的号码可以显示3次。您可以在Android Account Manager

找到更多信息

您可以使用 ContactsContract.RawContacts.ACCOUNT_TYPE 列来过滤和检索仅与单个帐户关联的联系人。

String[] projection = new String[] {
                    ContactsContract.RawContacts._ID,
                    ContactsContract.RawContacts.ACCOUNT_TYPE,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Photo.CONTACT_ID };


            String selectionFields =  ContactsContract.RawContacts.ACCOUNT_TYPE + " = ?";
            String[] selectionArgs = new String[]{"com.google"};

            Cursor cursor =  getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    selectionFields,
                    selectionArgs,
                    ContactsContract.Contacts.DISPLAY_NAME
            );

在此代码中,仅选择与Google帐户关联的联系人。同样,如果您只想获取WhatsApp联系人列表,可以将&#34; com.google&#34; 替换为&#34; com.whatsapp&#34;

答案 2 :(得分:0)

  

我认为问题是因为联系人列表同步不同   帐户。

是的,您的联系人列表同步了多个帐户。您应该使用帐户类型过滤联系人:mvn clean install jacoco:prepare-agent jacoco:report 您可以研究的帐户: https://developer.android.com/reference/android/accounts/AccountManager.html

答案 3 :(得分:0)

您可以搜索namespace org.gov.budget asset Tax identified by Id{ o String Id --> TaxPayer payer o Double amount o Integer year o Boolean processed } asset BudgetAccount identified by Id{ o String Id o Double amount } participant Government identified by Id{ o String Id --> BudgetAccount account } participant TaxPayer identified by PANID{ o String PANID o String name o Double income o Integer taxSlab } transaction PayTax{ -->Tax tax -->Government gov } Here is the implementation for the transaction. async function payTax(tax){ tax.tax.amount = tax.tax.payer.income*tax.tax.payer.taxSlab*0.05; tax.gov.account.amount+=tax.tax.amount; tax.tax.processed = true; let assetRegistry = await getAssetRegistry('org.gov.budget.BudgetAccount'); await assetRegistry.update(tax.gov.account); assetRegistry = await getAssetRegistry('org.gov.budget.Tax'); await assetRegistry.update(tax.tax); } 联系人,而不是所有RawContacts。这只会给您1个具有给定名称的联系人(例如在“联系人”应用中)。 示例(更改代码):

rule abc{
  description: "Grant business network administrators full access to system resources"
    participant: "org.gov.budget.TaxPayer"
    operation: READ
    resource: "org.hyperledger.composer.system.ParticipantRegistry"
    action: ALLOW
}

rule abc4{
  description: "Grant business network administrators full access to system resources"
    participant: "org.gov.budget.TaxPayer"
    operation: READ
    resource: "org.gov.budget.Government"
    action: ALLOW
}

来源:https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html