Java 如何从光标中删除唯一值

Java 如何从光标中删除唯一值,java,android,cursor,android-contacts,Java,Android,Cursor,Android Contacts,我正在开发一个应用程序,在这个应用程序中,我只应该向用户显示重复的电话联系人,这样他们就可以删除重复的联系人。 到目前为止,我可以使用以下代码显示所有联系人: 主要活动: private void showContacts() { Cursor cursor = ContactHelper.getContactCursor(getContentResolver(),""); String[] fields = new String[]{ContactsContract.Data.DISPLAY

我正在开发一个应用程序,在这个应用程序中,我只应该向用户显示重复的电话联系人,这样他们就可以删除重复的联系人。

到目前为止,我可以使用以下代码显示所有联系人:

主要活动:

private void showContacts() {
Cursor cursor = ContactHelper.getContactCursor(getContentResolver(),"");
String[] fields = new String[]{ContactsContract.Data.DISPLAY_NAME};

adapter =new SimpleCursorAdapter(this,android.R.layout.simple_list_item_multiple_choice,cursor,fields,new  int[]{android.R.id.text1});
listView.setAdapter(adapter);
adapter.notifyDataSetChanged(); }
public static Cursor getContactCursor(ContentResolver contactHelper,
                                      String startsWith) {

    String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor cur = null;

    try {
        if (startsWith != null && !startsWith.equals("")) {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " like \"" + startsWith + "%\"", null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        } else {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection, null, null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        }
        cur.moveToFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cur;
}

private static long getContactID(ContentResolver contactHelper,
                                 String number) {
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));

    String[] projection = { ContactsContract.PhoneLookup._ID };
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, null, null,
                null);

        if (cursor.moveToFirst()) {
            int personID = cursor.getColumnIndex(ContactsContract.PhoneLookup._ID);
            return cursor.getLong(personID);
        }

        return -1;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }

    return -1; }
ContactHelper.GetContactsCursor函数:

private void showContacts() {
Cursor cursor = ContactHelper.getContactCursor(getContentResolver(),"");
String[] fields = new String[]{ContactsContract.Data.DISPLAY_NAME};

adapter =new SimpleCursorAdapter(this,android.R.layout.simple_list_item_multiple_choice,cursor,fields,new  int[]{android.R.id.text1});
listView.setAdapter(adapter);
adapter.notifyDataSetChanged(); }
public static Cursor getContactCursor(ContentResolver contactHelper,
                                      String startsWith) {

    String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor cur = null;

    try {
        if (startsWith != null && !startsWith.equals("")) {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " like \"" + startsWith + "%\"", null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        } else {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection, null, null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        }
        cur.moveToFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cur;
}

private static long getContactID(ContentResolver contactHelper,
                                 String number) {
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));

    String[] projection = { ContactsContract.PhoneLookup._ID };
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, null, null,
                null);

        if (cursor.moveToFirst()) {
            int personID = cursor.getColumnIndex(ContactsContract.PhoneLookup._ID);
            return cursor.getLong(personID);
        }

        return -1;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }

    return -1; }
如何从光标中筛选唯一值?如果可能的话,只根据电话号码保留重复的联系人?

可能会这样做

    try {
        if (startsWith != null && !startsWith.equals("")) {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " like \"" + startsWith + "%\""
                            + " AND "
                            + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " IN (SELECT " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " as name FROM view_data GROUP BY " +ContactsContract.CommonDataKinds.Phone.NUMBER + " HAVING COUNT(name)>1)",
                    null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        } else {
            cur = contactHelper.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " IN (SELECT " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " as name FROM view_data GROUP BY " +ContactsContract.CommonDataKinds.Phone.NUMBER + " HAVING COUNT(name)>1)",
                    null,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
        }
        cur.moveToFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }