Android根据电话号码和显示名称

时间:2015-08-18 13:16:29

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

我试图在电话号码和姓名的基础上获得不同的联系人。我得到了所有的联系,但得到了重复。这是我的代码

  Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    String[] projection = {
            ContactsContract.Data.CONTACT_ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.Data.DATA1
    };


Cursor phones = getContentResolver().query(
                uri, projection,ContactsContract.Data.MIMETYPE + "='" + `ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''", null, null);// uri, projection,selection+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''", null, null);`
if (phones.moveToFirst()) {

            do {
                long ID = phones.getLong(phones.getColumnIndex(projection[0]));
                String DisplayName = phones.getString(phones.getColumnIndex(projection[1]));
                String Key = phones.getString(phones.getColumnIndex(projection[2]));

                if (Key != null && Key.toString().trim().length() > 0 && (Key.startsWith("0") || Key.startsWith("+"))) {
                    ContactBean contactModel = new ContactBean(DisplayName, Key, "");



                    list.add(contactModel);
                } else {
                    // No number!!
                }
            } while (phones.moveToNext());
        }

获得明确联系人的任何帮助。

我的预测已经应用了Distinction。当我尝试像这样对

应用不同的查询时
  String selection = " 'SELECT DISTINCT " + ContactsContract.Data.DISPLAY_NAME+"'";

我收到了这个错误。

SELECT DISTINCT contact_id, display_name, data1 FROM view_data_restricted data LEFT OUTER JOIN (SELECT data_usage_stat.data_id as STAT_DATA_ID, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_usage_stat.data_id) as data_usage_stat ON (STAT_DATA_ID=data._id) WHERE (1) AND (( 'SELECT DISTINCT contact_id'mimetype='vnd.android.cursor.item/phone_v2' AND data1!=''))

这表明我的投影已经在应用选择。但为什么它不起作用。任何解决方案?

1 个答案:

答案 0 :(得分:0)

// 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();

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