【问题标题】:Showing some contacts multiple times in my App from phone book从电话簿中的我的应用程序中多次显示一些联系人
【发布时间】:2019-01-21 06:52:09
【问题描述】:

我在我的应用中获得了三到两次相同的联系人,这发生在一些联系人而不是每个联系人身上。在我的应用程序中,一切都按预期工作,但是当点击我的显示联系人时,它会显示三个相同的联系人,但在手机联系人中只存储一次。我尝试了一切,但无法解决这个问题,任何人都可以帮助我。或者是否有任何替代方法。

这是我的代码:-

    @Override
protected Integer doInBackground(Void... params) {


    try {


        db = new WhooshhDB(myContext);
        this.list = new ArrayList<>();

        ContentResolver cr = myContext.getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
        if ((cur != null ? cur.getCount() : 0) > 0) {

            while (cur != null && cur.moveToNext()) {

                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));

                if (cur.getInt(cur.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));

                        ContactModel model = new ContactModel();

                        if (phoneNo.replaceAll("\\s", "").trim().length() > 7) {
                            model.name = name;
                            model.mobileNumber = phoneNo.replaceAll("\\s", "");
                            if (model.mobileNumber.contains("-")) {
                                model.mobileNumber = model.mobileNumber.replaceAll("-", "");
                            }
                            model.iconHexColor = AppConstant.getRandomSubscriptionHexColor(myContext);
                            if (!phoneNumber.equals(model.mobileNumber)) {
                                list.add(model);
                            }

                        }

                        Log.i("FetchContacts", "Name: " + name);
                        Log.i("FetchContacts", "Phone Number: " + phoneNo);
                    }
                    pCur.close();
                }
            }
        }
        if (cur != null) {
            cur.close();
        }

        return AppConstant.SUCCESS;
    } catch (Exception ex) {
        return null;
    }
}

【问题讨论】:

    标签: java android android-contacts contactscontract


    【解决方案1】:

    您正在为每个联系人的每个电话打印这些“FetchContacts”日志,因此如果一个联系人为她存储了多个电话,您会看到它打印了多次(即使它是同一个电话号码)。

    如果您安装了诸如 Whatsapp 之类的应用程序,那么您几乎总是会看到每个联系人的所有电话号码都重复,从而导致这些日志被打印的次数超过每个联系人一次。

    此外,通过手机获取联系人是一种缓慢而痛苦的方式。 相反,您可以简单地通过 Phones.CONTENT_URI 直接查询并获取数据库中的所有电话,并通过 Contact-ID 将它们映射到联系人中:

    Map<String, List<String>> contacts = new HashMap<String, List<String>>();
    
    String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
    Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null);
    
    while (cur != null && cur.moveToNext()) {
        long id = cur.getLong(0); // contact ID
        String name = cur.getString(1); // contact name
        String data = cur.getString(2); // the actual info, e.g. +1-212-555-1234
    
        Log.d(TAG, "got " + id + ", " + name + ", " + data);
    
        // add info to existing list if this contact-id was already found, or create a new list in case it's new
        String key = id + " - " + name;
        List<String> infos;
        if (contacts.containsKey(key)) {
            infos = contacts.get(key);
        } else {
            infos = new ArrayList<String>();
            contacts.put(key, infos);
        }
        infos.add(data);
    }
    
    // contacts will now contain a mapping from id+name to a list of phones.
    // you can enforce uniqueness of phones while adding them to the list as well.
    

    【讨论】:

      【解决方案2】:

      摆脱

      while (cur != null && cur.moveToNext()) { 
      

      改成

      if(cur.moveToFirst()){
      list.clear();
      

      【讨论】:

      • 现在我只能从电话簿中获得 1 个联系人
      • 这不是你想要的吗?
      • 否 它只给列表中的一个联系人
      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2012-08-20
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多