联系游标返回重复的行

时间:2015-12-15 07:20:09

标签: android android-contacts

我的应用程序需要联系人进行集成。我正在编写一个查询来获取本机的联系人。我得到了所有的行但是有重复的行。如果连接Whatsapp,Skype和其他帐户,将会重复。

以下是查询

String isPhoneType = "(" + Data.MIMETYPE + "='" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
            + "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL ) ";

    String query = Contacts.DISPLAY_NAME + " IS NOT NULL "
            + " AND (" + isPhoneType + ")" ;



    String[] projection;
    if (Compatibility.isCompatible(11)) {
        projection = new String[] {
                Data._ID,
                Data.CONTACT_ID,
                Data.DATA1,
                Data.DISPLAY_NAME,
                Data.PHOTO_ID,
                Data.LOOKUP_KEY,
                Data.PHOTO_URI
        };
    } else {
        projection = new String[] {
                Data._ID,
                Data.CONTACT_ID,
                Data.DATA1,
                Data.DISPLAY_NAME,
                Data.PHOTO_ID,
                Data.LOOKUP_KEY
        };
    }

    Cursor resCursor = ctxt.getContentResolver().query(uri,
            projection, query,
            null, Data.DISPLAY_NAME + " ASC");

请根据电话号码获取唯一的行。

由于

1 个答案:

答案 0 :(得分:0)

unique添加到sqlite数据库中的电话号码列,您将无法获得两次相同的联系。

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT UNIQUE," + KEY_IMAGE + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}