【问题标题】:how to select unique contacts from android如何从android中选择唯一的联系人
【发布时间】:2012-11-10 13:18:19
【问题描述】:

我想从 android 中选择 唯一的联系人,只选择那些有电话号码的联系人。我正在使用此代码

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, ContactsContract.Contacts.DISPLAY_NAME);
        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.mainListView);

        // When item is tapped, toggle checked properties of CheckBox and
        // Planet.
        mainListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener()
                {
                    public void onItemClick(AdapterView<?> parent, View item,
                            int position, long id)
                    {
                        ContactsList planet = listAdapter.getItem(position);
                        planet.toggleChecked();
                        PlanetViewHolder viewHolder = (PlanetViewHolder) item
                                .getTag();
                        viewHolder.getCheckBox().setChecked(planet.isChecked());
                    }
                });

        // Create and populate planets.
        planets = (ContactsList[]) getLastNonConfigurationInstance();
        // planets = new Planet[10];
        // planets.Add("asdf");
        ArrayList<ContactsList> planetList = new ArrayList<ContactsList>();
        String phoneNumber = null;
        String phoneType = null;

        count = cur.getCount();
        contacts = new ContactsList[count];

        if (planets == null)
        {
            if (cur.getCount() > 0)
            {
                planets = new ContactsList[cur.getCount()];
                int i = 0;
                //
                while (cur.moveToNext())
                {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                    {
                        // Query phone here. Covered next
                        Cursor pCur = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[]
                                        { id }, null);

                        // WHILE WE HAVE CURSOR GET THE PHONE NUMERS
                        while (pCur.moveToNext())
                        {
                            // Do something with phones
                            phoneNumber = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));

                            phoneType = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                            Log.i("Pratik", name + "'s PHONE :" + phoneNumber);
                            Log.i("Pratik", "PHONE TYPE :" + phoneType);
                        }
                        pCur.close();
                    }

                    planets = new ContactsList[]
                    { new ContactsList(name, phoneNumber) };

                    contacts[i] = planets[0];
                    planetList.addAll(Arrays.asList(planets));

                    i++;
                }
            }

此代码检索所有联系人并将其放入列表中。但我想要唯一的联系人,并且只有那些有电话号码的联系人。我怎样才能做到这一点??有没有什么方法可以在查询中传递一些参数来只选择唯一的联系人???

【问题讨论】:

    标签: android contacts android-contentprovider android-contacts android-cursor


    【解决方案1】:

    我想你的意思是你有一些联系人的重复记录。因此,您必须为查询添加条件。重要的是联系人必须在可见组中并且有电话号码

    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                    + ("1") + "'";
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                    + " COLLATE LOCALIZED ASC";
    cur = context.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, projection, selection
                            + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                            + "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
    

    2020 年 5 月 20 日更新

      suspend fun fetchContacts(): ArrayList<FriendItem> {
            val list = ArrayList<FriendItem>()
            val uri: Uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
            val selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
            val cursor: Cursor? = context.contentResolver.query(
                uri,
                arrayOf(
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone._ID,
                    ContactsContract.Contacts._ID
                ),
                selection,
                null,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
            )
    
            cursor?.let {
                val nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
                val phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                while (cursor.moveToNext()) {
                    val info = FriendItem(
                        friendName = cursor.getString(nameIndex),
                        friendPhoneNumber = cursor.getString(phoneIndex)
                    )
                    list.add(info)
                }
                cursor.close()
            }
            return list
        }
    

    【讨论】:

    • @Puru:我在Android2.2上用过这个
    • @Jul : ICS 及以上版本有解决方案吗?
    • @NamTrung : 先生,您能否将光标的示例代码发布在while循环中以访问DISPLAY_NAMENUMBER
    • @NamTrung 以下代码不适用于我的情况:while (cursor.moveToNext()) { final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); final String mob = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    【解决方案2】:

    这有助于我联系电话号码。这里我们查询数据表,并使用 CONTACT_ID contact provider documentation

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    
    final String ORDER_BY = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " ASC";
    
        final String[] PROJECTION = {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
                ContactsContract.CommonDataKinds.Phone.NUMBER
        };
    
    return new CursorLoader(
                    context,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    PROJECTION,
                    null,
                    null,
                    ORDER_BY
            );
    }
    

    【讨论】:

      【解决方案3】:

      获取电话号码和联系人姓名的简单方法

      // set as global
      Set<string> phonenumbersList = new HashSet<string>();
      
                  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                  while (phones.moveToNext())
                  {
                  String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      
                  //contact has name number and phonenumber does not exists in list
                  if ( phoneNumber != null && name != null && !phonenumbersList.contains(phoneNumber)){ 
                      planets = new ContactsList[]{ new ContactsList(name, phoneNumber) };
      
                      phonenumbersList.add(phoneNumber);
                      planetList.addAll(Arrays.asList(planets));
                      planetList.Add(phoneNumber, name);
                  }
                  }
                  phones.close();
      

      【讨论】:

      • 我已经用我的代码获取电话号码。我想选择唯一的联系人
      • 我不想重复。一个电话号码只来一次
      • 但是你仍然可以有重复的名字,因为一个联系人可能有更多的电话号码,我会编辑我的帖子给我一个秒
      • 是的,一个联系人可以有多个号码,但一个号码只有一个名字。
      • 好的,看看我的编辑,在这个例子中你没有重复的电话号码
      猜你喜欢
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2011-09-28
      相关资源
      最近更新 更多