Android get phone contacts and remove duplicates

后端 未结 5 2044
天命终不由人
天命终不由人 2021-01-14 11:53

I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here\'s the code for it

  Uri uri = ContactsContract.Da         


        
5条回答
  •  忘掉有多难
    2021-01-14 12:24

    You can try this :

            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
            String lastnumber = "0";
    
            if (cur.getCount() > 0)
            {
                while (cur.moveToNext())
                {
                    String number = null;
                    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)
                    {
                        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]
                        { id }, null);
                        while (pCur.moveToNext())
                        {
                            number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            Log.e("lastnumber ", lastnumber);
                            Log.e("number", number);
    
                            if (number.equals(lastnumber))
                            {
    
                            }
                            else
                            {
                                lastnumber = number;
    
                                Log.e("lastnumber ", lastnumber);
                                int type = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
                                switch (type)
                                {
                                    case Phone.TYPE_HOME:
                                        Log.e("Not Inserted", "Not inserted");
                                        break;
                                    case Phone.TYPE_MOBILE:
    
                                        databaseHandler.insertContact(id, name, lastnumber, 0);
                                        break;
                                    case Phone.TYPE_WORK:
                                        Log.e("Not Inserted", "Not inserted");
                                        break;
                                }
    
                            }
    
                        }
                        pCur.close();
                    }
    
                }
            }
    

    Here i have inserted data in sqlite database first and then Write select query with group by name.

    Hope it helps

提交回复
热议问题