简体   繁体   English

Android检查值是否等于数组中的值

[英]Android check if value equals to the values in Array

I have a ArrayList tokens; 我有一个ArrayList标记; I am adding values to the array with a listView click. 我用listView单击将值添加到数组。 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

You are not adding when you initially have 0 tokens. 最初有0个令牌时,您没有添加。

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

You can simply check the existence using contains method. 您可以使用contains方法简单地检查是否存在。

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

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

If your list is empty, you never go into the loop, so you'll never call add . 如果列表为空,则永远不会进入循环,因此永远不会调用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: 或者,您可以将Set<String>用于:

// 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. 还要注意,您当前正在测试id_To_Search ,然后添加selectedToken此答案假设您实际上打算在两个地方都使用selectedToken

当tokens.size()为0时,for循环将不会执行。因此,您将永远不会添加令牌,因为最初的令牌列表为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM