发布于 5年前

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

我有一个数组列表.

1)我的第一个ArrayList some_num = new ArrayList();其中包含这样的值.

[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);
                    }
                }

我试图从移动电话簿中获取我的ArrayList中不存在的那些数字.我知道在条件中我必须获得数组的值,但是当我尝试这样做时,我没有得到其余的联系.

提前致谢

解决方案:

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

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

你的代码有什么问题呢?

您正在将数组与包含最后一个数字引用的数字变量进行比较.因为你只得到单一的结果.

如果您仍然想要使用您的代码,那么创建另一个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));
            } 
        } 

要获得User的完整详细信息,您可以创建Model类,其中包含如下所示的用户详细信息:

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;
    }
}

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

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

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

  String name   = people.getString(indexName);

现在商店名称和phonenumber是这样的:

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

希望这能解决你的问题.

©2020 edoou.com   京ICP备16001874号-3