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++;
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.