Mohammed Nasrullah
Mohammed Nasrullah

Reputation: 433

Android check if value equals to the values in Array

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

Upvotes: 0

Views: 2051

Answers (5)

frogatto
frogatto

Reputation: 29285

You can simply check the existence using contains method.

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

Upvotes: 3

Alexander
Alexander

Reputation: 48252

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);
}

Upvotes: 4

Luis Miguel Sierra
Luis Miguel Sierra

Reputation: 734

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);
            }
}

Upvotes: 3

Bunny Rabbit
Bunny Rabbit

Reputation: 8411

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500465

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.

Upvotes: 2

Related Questions