0

I have a ArrayList tokens; I am adding values to the array with a listView click. Before i add the value to array i check if the value already exist array. I remove the value is it exists else i will add the the value

This is how i have done, but the values are not being added to the array

ArrayList<String> tokens;
tokens = new ArrayList<String>();
...
....
public void onItemClick(AdapterView<?> listView, View view,
                            int position, long id) {
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);
        String selectedtoken = cursor.getString(cursor.getColumnIndexOrThrow("ContactToken"));

        for (int i = 0; i < tokens.size(); i++) {
                if (tokens.get(i).equals(id_To_Search)) {
                    tokens.remove(i);
                }
                else {
                    tokens.add(selectedtoken );
                }
            }
    }
...
...
Log.i("array: ", tokens.toString()); // No values in the array
Naz141
  • 433
  • 1
  • 8
  • 31

5 Answers5

4

You are not adding when you initially have 0 tokens.

Change to:

boolean removed = false;
for (Iterator<String> iter = tokens.iterator(); iter.hasNext(); ) {
    if (iter.next().equals(id_To_Search)) {
        iter.remove();
        removed = true;
    }
}
if(!removed) {
   tokens.add(selectedtoken);
}
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
3

You can simply check the existence using contains method.

if(!tokens.contains(id_To_Search)){
    tokens.add(selectedtoken);
} else {
    tokens.remove(selectedtoken);
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
3

You're checking for each element in your array if it's the item you're going to store/delete, and then doing the proper operation.

You should find first if you're element exists in the whole array, and then adding or deleting it.

Try something like this:

public void onItemClick(AdapterView<?> listView, View view,
                        int position, long id) {
    Cursor cursor = (Cursor) listView.getItemAtPosition(position);
    String selectedtoken = cursor.getString(cursor.getColumnIndexOrThrow("ContactToken"));

            if (tokens.contains(id_To_Search)) {     
               tokens.remove(id_To_Search);

            }
            else {
                tokens.add(id_To_Search);
            }
}
2

If your list is empty, you never go into the loop, so you'll never call add. If you do have any tokens to start with, you're either adding or removing the new token for each existing token which isn't what you want.

I suspect you want:

int existingIndex = tokens.indexOf(selectedToken);
if (existingIndex == -1) {
   tokens.add(selectedToken);
} else {
   tokens.remove(existingIndex);
}

Alternatively, you could use a Set<String> with:

// Speculatively try to remove it... and add it if you couldn't remove
boolean removed = tokens.remove(selectedToken);
if (!removed) {
   tokens.add(selectedToken);
}

Also note that you're currently testing for id_To_Search but then adding selectedToken - this answer assumes you actually meant to use selectedToken in both places.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The for loop will not execute when tokens.size() is 0. So you'll never add a token because initially the token list is empty.

Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106