【问题标题】:sql android ignore duplicate retrieving contentsql android忽略重复的检索内容
【发布时间】:2018-01-31 17:53:26
【问题描述】:

帮我解决这个问题以删除重复的联系人 我从数据库中获取

 cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                 ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''",null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

【问题讨论】:

  • 您正在查询电话表,因此即使它们是同一部电话并且属于同一联系人,您也会在每部电话中获得一行,请参阅上面链接中的我的答案解决方案

标签: android duplicates cursor contactscontract resolver


【解决方案1】:

使用此代码从移动数据库中获取联系人列表并在列表视图中设置

没有重复的数据检索。

public void customgetContactList()
{
    String SELECTION = "((" +
            ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
            ContactsContract.Data.DISPLAY_NAME + " != '"+checkDuplicacy+"' ))";


    Cursor cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
            SELECTION,null,"upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

    int ColumeIndex_ID = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
        int ColumeIndex_DISPLAY_NAME = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int ColumeIndex_HAS_PHONE_NUMBER = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
    while (cursor.moveToNext()){
        String id = cursor.getString(ColumeIndex_ID);
        String name = cursor.getString(ColumeIndex_DISPLAY_NAME);
        String has_phone = cursor.getString(ColumeIndex_HAS_PHONE_NUMBER);

        //phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
        if (!name.equals(checkDuplicacy) && !has_phone.equals(checkDuplicacynumber))
        {

            HashMap<String,String> hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("name",name);
            hashMap.put("phone",has_phone);
            arrayList.add(hashMap);
            String[] from = {"name","phone"};
            int[] to = {R.id.textViewName,R.id.textviewnumber};
            SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.customtextviewforlist,from,to);
          //  SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.customtextviewforlist,cursor,from,to);

            //startManagingCursor(cursor);
            contactListView.setAdapter(simpleAdapter);
            contactListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


            checkDuplicacy=name;
            checkDuplicacynumber=has_phone;
        }
    }
}

【讨论】:

  • 虽然这可能会为大多数设置返回正确的结果,但如果您的手机上有 2 个同名联系人,它会跳过其中一个。此外,如果您有多个电话号码的联系人,它只会显示该联系人的第一部电话。一般来说,我会避免在 Android 通讯录中使用 DISPLAY_NAME 作为唯一键。
  • 答案在这里:stackoverflow.com/a/47788324/819355 使用 CONTACT_ID 字段去重复结果,并且还支持每个联系人多个电话。这个问题与链接上的问题完全相同
  • 谢谢哥们。在您的帮助下,我能够修复它。
猜你喜欢
  • 1970-01-01
  • 2014-02-16
  • 2018-04-26
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2013-03-27
相关资源
最近更新 更多