I have an array list.
1) my first ArrayList<String> some_num = new ArrayList<String>();
which contain the value like this.
[1111111111, 2222222222]
Now I am trying to compare this with my mobile contact like this.
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 those number which is not present in my ArrayList from the mobile phone book. I know that in for condition i have to get the value of array but when i try to do this i am not getting the rest of my contact.
Thanks in advance
dont use any other loop to compare the numbers. What's wrong with your code then?
You are comparing your array with number
variable which holds the last number reference. because of which your are getting only single result.
if your still want to use your code then create another arrayList in which you store all the number like this:
ArrayList<String> numberList = new ArrayList<String>();
and to add the number in this list use below line before j++;
numberList.add(number);
Now update your last iterator block to work like this:
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 complete detail of User you can create the Model class which contains the user details like this:
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 have to use this model in your activity to get and set the details of user:
ArrayList<UserDetails> mUserDetailList = new ArrayList<UserDetails>();
and to get the contacts name use this code:
String name = people.getString(indexName);
now store name and phonenumber like this:
UserDetails mUserModel = new UserDetails();
mUserModel.setUserPhone(number);
mUserModel.setUserName(name);
mUserDetailList.add(mUserModel);
Now to check whether the number exists or not:
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 will solve your problem.