0

I have created the following method, to add a name and number for every contact within the user's phone book:

PlayerDetails contactPlayer = new PlayerDetails();

public PlayerList getContacts(){
    ContentResolver cr = calledActivity.getContentResolver();
    Cursor contactList = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
    while (contactList.moveToNext()){
        contactPlayer.name = contactList.getString(contactList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        contactPlayer.number = contactList.getString(contactList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        playerNameNumber.myPlayers.add(pos,contactPlayer);

        //playerNameNumber.add(pos,name + " " + phoneNumber);
        pos++;
    }
    return playerNameNumber;
}

But each position contains the same data. When I debug, I can see that during the while loop that it is going through each contact, at contactPlayer.name and number it has a different contact each time but at the : playerNameNumber.myPlayers.add it will add the data but to each position. So playerNameNumber[0] and playerNameNumber[1] have the same contact information?!

playerNameNumber is an object with the following:

ArrayList<PlayerDetails> myPlayers = new ArrayList<>();

    void addPlayer(PlayerDetails player){
        myPlayers.add(player);

        }

myPlayers:

   int id;
    String name;
    String number;

Is it the while loop that is causing this issue? also, I know I have 'pos' but I am sure I don't need this?

Why is it the 'add' is replacing every item in the arraylist with the same data?

  • It looks like you are adding the same object reference. After the `moveToNext()`, do you need to create a new contactPlayer and then set the name and number? – KevinO Apr 13 '18 at 16:54
  • Where is contactPlayer instanciated? Don't see it in the while loop and also you calling the add method of the list attribut myplayers (but why have a addPlayer method then) – TheBakker Apr 13 '18 at 16:56
  • 1
    Create a new instance when adding. – Compass Apr 13 '18 at 16:57
  • @KevinO - Yea I do –  Apr 13 '18 at 17:00
  • But I thought the while loop, would go through each contact and then use get index to get the next contact details? –  Apr 13 '18 at 17:01
  • @Compass a new instance of? Sorry Im new to this –  Apr 13 '18 at 17:01
  • 1
    [https://stackoverflow.com/questions/5126082/what-exactly-is-an-instance-in-java](https://stackoverflow.com/questions/5126082/what-exactly-is-an-instance-in-java) – Bö macht Blau Apr 13 '18 at 17:05

2 Answers2

1

For same contactPlayer you are assigning new name and number. Can't see where its instantiated in the loop. So everytime they are added to the list all are pointing to the same Object and will contain the name and number added last in the loop.

  • It is added globally within the class, But I thought that during the while loop it would set it to the new name and number? –  Apr 13 '18 at 17:31
1

You will have to instantiate the PlayerDetails every time you run through while loop otherwise it will point to same location (The Last Contact).

public PlayerList getContacts(){
    ContentResolver cr = calledActivity.getContentResolver();
    Cursor contactList = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
    while (contactList.moveToNext()){
        contactPlayer = new PlayerDetails();
        contactPlayer.name = contactList.getString(contactList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        contactPlayer.number = contactList.getString(contactList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        playerNameNumber.myPlayers.add(pos,contactPlayer);

        //playerNameNumber.add(pos,name + " " + phoneNumber);
        pos++;
    }
    return playerNameNumber;
}
Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
  • It is added globally within the class, But I thought that during the while loop it would set it to the new name and number? –  Apr 13 '18 at 17:31