从联系人中删除号码

时间:2015-01-22 12:40:37

标签: android contact

以下代码可让我显示重复的联系人。 当我尝试删除时,它会删除重复的数字以及原始数字。 我希望它只删除列表视图中存在的重复数字。

这是我的代码。

public class MainActivity extends Activity {

    ListView listView;
    ArrayList<String> listItems = new ArrayList<String>();
    Set<String> dupesRemoved = new HashSet<String>();
    String[] newList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);


        String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
        Cursor curLog =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,order);
        Cursor cursor = null;

        if(curLog != null) {
            while(curLog.moveToNext()) {
                String str = curLog.getString(curLog.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                //contactid = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
                listItems.add(str);
            }
        }
        dupesRemoved = findDuplicates(listItems);
        String listString = dupesRemoved.toString();
        listString = listString.substring(1,listString.length()-1);
        newList = listString.split(", ");
        //Arrays.sort(newList);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, newList);
        listView.setAdapter(adapter);

    }


    public void deleteDupes(View view) {
        String[] info = new String[2];

        for (String s : newList) {
          info = (getContactInfo(s));

          updateContact(info[0],this,info[1]);

        listView.invalidateViews();
        }
    }




        public void updateContact(String contactId, Activity act, String type){

            /* ASSERT: @contactId alreay has a work phone number */
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='"  +
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";
            String[] phoneArgs = new String[]{contactId,type /*String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)*/};

            ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
                    .withSelection(selectPhone, phoneArgs).build());
        try {
            act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
    }

    public static Set<String> findDuplicates(List<String> listContainingDuplicates) {

        final Set<String> setToReturn = new HashSet<String>();
        final Set<String> set1 = new HashSet<String>();

        for (String yourInt : listContainingDuplicates) {
            if (!set1.add(yourInt)) {
                setToReturn.add(yourInt);
            }
        }
        return setToReturn;
    }
    private String[] getContactInfo(String number)
    {
        String[] contactInfo = new String[2];

        ContentResolver context = getContentResolver();

        /// number is the phone number
        Uri lookupUri = Uri.withAppendedPath(
                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));

        String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.TYPE };

        Cursor cur = context.query(lookupUri,mPhoneNumberProjection, null, null, null);
        try
        {
            if (cur.moveToFirst())
            {
                contactInfo[0] = cur.getString(0);
                contactInfo[1] = cur.getString(2);
                return contactInfo;


            }
        }
        finally
        {
            if (cur != null)
                cur.close();
        }
        return contactInfo;
    }


}

2 个答案:

答案 0 :(得分:0)

使用hashmap而不是arraylist来存储键值对中的项目,因为地图中不允许使用重复键。

答案 1 :(得分:0)

删除联系人的Java代码:

public static boolean deleteContact(Context ctx, String phone, String name) {
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
    try {
        if (cur.moveToFirst()) {
            do {
                if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
                    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    ctx.getContentResolver().delete(uri, null, null);
                    return true;
                }

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    return false;
}

明确许可:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />