在 Android 中获取不同联系人的最佳方法是什么 [英] What is the best way to get distinct contacts in Android

查看:20
本文介绍了在 Android 中获取不同联系人的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过此代码成功将联系人存储在 parse.com 仪表板数据浏览器中.

I am successfully storing contacts in parse.com dashboard data browser by this code.

public void readContacts(){
         ContentResolver cr = getContentResolver();
         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

         if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                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))) ==1) {
                    System.out.println(name );
                    ParseObject testObject = new ParseObject("Contacts");

                    testObject.put("names", name);

                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                           new String[]{id}, null);
                    while (pCur.moveToNext()) {
                          String phone = pCur.getString(
                                 pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         System.out.println( phone);
                        testObject.put("phonenumber", phone);

                    }
                    pCur.close();
                    testObject.saveInBackground();
           }
        }
     }
  }

但是没有检查重复的联系人!

But there is no check for the duplicate contacts !

它存储从 SIM/手机内存中复制的所有联系人.

It stores all the contacts duplicate from sim / phone memory.

如何避免?

我认为一种可能的方法是在本地数据库中存储不同的名称(联系人),&然后检索该数据以将其存储在 parse.com

有没有更好的方法?

提前致谢...

推荐答案

一种简单的方法是将数据加载到没有重复数据的 MatrixCursor.例如,假设您有一个游标 c1 将有许多联系人,但您需要一个没有重复数据的游标.您可以这样做:

An easy approach could be to load the data to a MatrixCursor with no duplicate data. For example lets assume you have a cursor c1 will many contacts, but you need a cursor with no duplicate data. Here is what you could do:

MatrixCursor mc = new MatrixCursor(new String[] { 
                        Phone._ID, 
                        Phone.DISPLAY_NAME_PRIMARY,
                        Phone.NUMBER
});

String lastNumber = "";

while(c1.moveToNext()){
    String id = c1.getString(c1.getColumnIndexOrThrow(Phone._ID));
    String name = c1.getString(c1.getColumnIndexOrThrow(Phone.DISPLAY_NAME_PRIMARY)));
    String number = c1.getString(c1.getColumnIndexOrThrow(Phone.NUMBER));

    //Some condition to check previous data is not matched and only then add row
    if(!lastNumber.contains(number)){
            lastNumber = number;
            mc.addRow(new String[]{id, name, number});
    }


}

c1.close();

创建一个具有相同列的 MatrixCursor 实例,如果最后一个号码或联系人姓名与前一个联系人的号码或联系人姓名不匹配,则加载.检查条件由您决定.按某种顺序查询数据,以便重复的联系人首先保持在一起.

Make an instance of MatrixCursor with same columns, and then load if last number or contact name does not match that of the previous contact. The condition for checking is upto you. Query data in some order so that the duplicate contacts stay together first.

加载 MatrixCursor 后,您可以从中获取数据.您还可以通过自定义 CursorLoader 或 CursorAdapter 将其附加到视图.

Once the MatrixCursor is loaded you can fetch data from it. You could also attach it to a view through a custom CursorLoader or CursorAdapter.

这篇关于在 Android 中获取不同联系人的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆