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
1
  • 1
    i would suggest use a set and then convert the set to the arraylist when you are done,it would be much easier Commented Nov 2, 2015 at 13:55

5 Answers 5

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

That will fail with a ConcurrentModificationException in many cases... and it will always have the selected token at the end.
I've corrected the 2nd but the 1st ehhh, it all on the UI thread
Just because it's in one thread doesn't mean you won't get a ConcurrentModificationException. See stackoverflow.com/questions/223918
Actually, my mistake - you're not using an enhanced for loop, so you won't get that error. But you will skip over entries at the moment. (If you remove entry 0, you're then not going to look at the "new" entry 0...)
Thank you, I've changed in accordance.
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);
            }
}

Comments

3

You can simply check the existence using contains method.

if(!tokens.contains(id_To_Search)){
    tokens.add(selectedtoken);
} else {
    tokens.remove(selectedtoken);
}

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.