如何将数组列表与移动联系人进行比较

How to compare an Array List with mobile Contact

我有一个数组列表。

1) 我的第一个 ArrayList<String> some_num = new ArrayList<String>(); 包含这样的值。

[1111111111, 2222222222] 

现在我正在尝试将其与我的手机联系人进行比较。

  Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
  String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};

                Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);

                int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                int j_count=0;
                String number;
                people.moveToFirst();
                do {

                    String name   = people.getString(indexName);
                     number = people.getString(indexNumber);
                    j_count++;


                    // Do work...
                } while (people.moveToNext());




                for(int j=0;j<=j_count;j++){

                    if (some_num.contains(number)){
                        // do nothing
                    }
                    else{
                    Log.e("num",number);
                    }
                }

我正在尝试从移动 phone 书中获取我的 ArrayList 中不存在的那些数字。我知道在 for 条件下我必须获取数组的值但是当我尝试这样做时我没有得到我的其余联系人。

提前致谢

一个理想的解决方案是使用 hasp 映射

将整个联系人存储在哈希映射中作为键 并通过简单地尝试将值放入哈希映射来检查您的列表是否包含这些数字。

这将起作用,因为哈希映射不会有重复项,当您尝试插入重复键时,您会知道

编辑 我认为您的代码可能适用于此修复程序

               String number;
                people.moveToFirst();
                do {

                    String name   = people.getString(indexName);
                     number = people.getString(indexNumber);
                 // for(int j=0;j<=j_count;j++){

                    if (some_num.contains(number)){
                        // do nothing
                    }
                    else{
                    Log.e("num",number);
                    //}
                    j_count++;


                    // Do work...
                } while (people.moveToNext());





                }

不要使用任何其他循环来比较数字。参考以下代码:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
  String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};

                Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);

                int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                int j_count=0;
                String number;
                people.moveToFirst();
                do { 

                    String name   = people.getString(indexName);
                     number = people.getString(indexNumber);

                      if (some_num.contains(number)){
                      }else{
                      }

                    j_count++;


                    // Do work... 
                } while (people.moveToNext());

那你的代码有什么问题?

您正在将您的数组与保存最后一个数字引用的 number 变量进行比较。因此,您只能得到一个结果。

如果您仍想使用您的代码,则创建另一个 arrayList,在其中存储所有数字,如下所示:

ArrayList<String> numberList = new ArrayList<String>();

并在 j++ 之前使用下行添加此列表中的数字;

numberList.add(number);

现在更新您的最后一个迭代器块,使其像这样工作:

       for(int j=0;j<numberList.siz();j++){

            if (some_num.contains(numberList.get(i)){
                // do nothing 
            } 
            else{ 
            Log.e("num",numberList.get(i));
            } 
        } 

要获得用户的完整详细信息,您可以创建包含用户详细信息的模型 class,如下所示:

public class UserDetails{
    private String userName;
    private String userPhone;
    private String userImage;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getUserImage() {
        return userImage;
    }

    public void setUserImage(String userImage) {
        this.userImage = userImage;
    }
}

现在您必须在 activity 中使用此模型来获取和设置用户的详细信息:

ArrayList<UserDetails> mUserDetailList = new ArrayList<UserDetails>();

并使用此代码获取联系人姓名:

  String name   = people.getString(indexName);

现在像这样存储姓名和电话号码:

UserDetails mUserModel = new UserDetails();

mUserModel.setUserPhone(number);
mUserModel.setUserName(name);

mUserDetailList.add(mUserModel);

现在查询号码是否存在:

 for(int j=0;j<mUserDetailList.siz();j++){

            if (some_num.contains(mUserDetailList.get(i).getUserPhone()){ 
                // do nothing  
            }  
            else{  
            Log.e("num",numberList.get(i).getUserPhone());
            Log.e("name",numberList.get(i).getUserName());
            }  
        }  

希望这能解决您的问题。

尝试如下操作,再次使用时不要忘记清除列表

ArrayList<String>  list = new ArrayList<String>();

 people.moveToFirst();
                do {

                    String name   = people.getString(indexName);
                     number = people.getString(indexNumber);
                     list.add(number);
                    j_count++;


                    // Do work...
                } while (people.moveToNext());

for(String str:list)
{

  if(yournumber.equalsIgnoreCase(str))
{ 
    //do your stuff
}

}