联系方式两次返回

时间:2013-11-01 08:41:16

标签: java android android-contentprovider android-contacts

我正在尝试获取联系人详细信息,例如姓名,电话号码,电子邮件和照片。附在arraylist的联系人。

但对于同时拥有电话号码和电子邮件地址的联系人。我最初可以通过电子邮件地址和电子邮件地址两次看到相同的联系人姓名。那么它的电话号码并没有显示为单个联系人(它应该是)。 有人可以帮我解决这个问题吗? TIA:)

参考:

public ArrayList<User> getPhoneContact(String paramString, ArrayList<User> paramArrayList)
    throws CustomException
  {

        //Cursor localCursor = null;
        Cursor cursor = null;
        ArrayList localArrayList = new ArrayList();
        User user;
        boolean flag;
        String s1;
        String s2;
        String s6;
        String s5;
        String s3;
        String s4;
        int i;
        int j;
        int k;
        int l;
        int i1;
        try
        {

            cursor = getNamesAndPictures(paramArrayList);

            if(cursor != null && cursor.moveToFirst()){
                user = null;

                i = cursor.getColumnIndex("data1");
                j = cursor.getColumnIndex("contact_id");
                k = cursor.getColumnIndex("display_name");
                l = cursor.getColumnIndex("data1");
                i1 = cursor.getColumnIndex("mimetype");
                s1 = null;

                do{
                    s2 = cursor.getString(j);
                    if(s2 == null)
                        return localArrayList;
                    //if(s2.equals(s1))
                        //return localArrayList;
                    user = new User();

                    s1 = s2;
                    s3 = cursor.getString(k);
                    user.setName(s3);
                    user.setContactId(s2);
                    user.setContactType(paramString);
                    s4 = cursor.getString(i1);
                    if(s4 != null){
                        if(s4.equals("vnd.android.cursor.item/phone_v2")){
                            s6 = cursor.getString(i);
                            user.setPhone(s6);
                        }

                        else if(s4.equals("vnd.android.cursor.item/email_v2")){
                                s5 = cursor.getString(l);
                                user.setEmail(s5);

                            }
                    }

                    localArrayList.add(user);
                }while(cursor.moveToNext());

            }
        }
        catch (Exception localException)
        {
          //localException
        }

    finally
    {
      //closeCursor(localCursor);
      closeCursor(cursor);
      closeDatabase();
    }

        return localArrayList;
  }

并且:

private Cursor getNamesAndPictures(ArrayList<User> paramArrayList)
  {
    String str1 = prepareContactIdsString(paramArrayList);
    ContentResolver localContentResolver = this.getAppContext().getContentResolver();
    String[] arrayOfString = { "data1", "contact_id", "display_name", "_id", "data1", "mimetype" };
    String str2 = "display_name != 'null' AND ( (mimetype = 'vnd.android.cursor.item/phone_v2'  AND is_primary != -1 )  OR (mimetype = 'vnd.android.cursor.item/email_v2'  AND is_primary != -1 ) ) AND contact_id NOT IN ( " + str1 + ")";
    return localContentResolver.query(android.provider.ContactsContract.Data.CONTENT_URI, arrayOfString, str2, null, "display_name COLLATE LOCALIZED ASC");
  }

1 个答案:

答案 0 :(得分:1)

这是因为您查询包含数据行的Data表,每行都有关于联系人的某种信息,例如一行用于电子邮件,一行用于电话号码。 如果你只想获得联系人,你应该查询ContactsContract.Contacts表,但是你必须查询他们每个人的电子邮件和电话。

http://developer.android.com/guide/topics/providers/contacts-provider.html