Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare an Array List with mobile Contact

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

like image 447
Shubhank Gupta Avatar asked Nov 20 '15 06:11

Shubhank Gupta


People also ask

How do you compare elements in an ArrayList?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

Can we compare array and List in Java?

Yes, you can iterate through the array and arraylist and compare elements. ArrayList elements are accessed with . get(index) .

How do you check if an ArrayList contains a value in Java?

contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.


3 Answers

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.

like image 162
Anjali Avatar answered Oct 26 '22 03:10

Anjali


One ideal solution could be to use a hasp map.

Store the entire contact in a hash map as the key and check if your list has those number by simply trying to put the value to the hash map.

This will work as hash map wont have duplicatesand when u try to insert a duplicate key you will know 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());





                }
like image 44
Abx Avatar answered Oct 26 '22 02:10

Abx


try as follows and dont forget to clear the list when ever 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
}

}
like image 22
venkatesh venkey Avatar answered Oct 26 '22 02:10

venkatesh venkey