是什么让Android中不同的联系人的最佳途径 [英] What is the best way to get distinct contacts in Android

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

问题描述

我成功地存储在parse.com仪表板数据浏览器接触这个code。

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

时存在一个更好的办法?

Is there exists a better way ?

在此先感谢...

推荐答案

这是很容易的办法是将数据加载到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的实例有相同的列,然后加载,如果最后一个数字或联系人姓名不匹配的previous接触。对于检查的条件是高达你。在一些为了查询数据,以便重复的联系人留下的第一在一起。

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天全站免登陆