How to compare a list of arrays with a mobile contact

I have a list of arrays.

1) my first ArrayList<String> some_num = new ArrayList<String>(); which contains a value similar to this.

 [1111111111, 2222222222] 

Now I am trying to compare this with my mobile contact as follows.

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

I am trying to get the number that is not in my ArrayList from the phone book. I know that for the condition I have to get the value of the array, but when I try to do this, I do not get the rest of my contact.

Thank you in advance

+5
source share
3 answers

Do not use any other loop to compare numbers. see the following code:

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

What happened to your code?

You compare your array with the variable number , which contains the last reference to the number. because of what you get only one result.

if you still want to use your code, then create another array in which you store the whole number like this:

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

and add the number to this list, use the line below j ++;

 numberList.add(number); 

Now update your last iterator block to work as follows:

  for(int j=0;j<numberList.siz();j++){ if (some_num.contains(numberList.get(i)){ // do nothing } else{ Log.e("num",numberList.get(i)); } } 

To get full user information, you can create a Model class that contains user data, such as:

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

Now you must use this model in your activity in order to obtain and establish user information:

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

and to get the name of the contact, use this code:

  String name = people.getString(indexName); 

now save the name and phone number, for example:

 UserDetails mUserModel = new UserDetails(); mUserModel.setUserPhone(number); mUserModel.setUserName(name); mUserDetailList.add(mUserModel); 

Now, to check if a number exists:

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

Hope this solves your problem.

+6
source

One ideal solution would be to use a hasp map.

Save the entire contact on the hash map as a key and check if your list has these numbers by simply putting this value in the hash map.

This will work because the hash map will not have duplicates, and when you try to insert a duplicate, you will find out that

EDIT I think your code might work with this fix.

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

try the following and remember to clear the list when reusing it

 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 } } 
+1
source

Source: https://habr.com/ru/post/1236396/


All Articles