Java 如果项目不存在于列表中,则将项目添加到数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22132970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:48:56  来源:igfitidea点击:

Add item to arraylist if it does not already exist in list

javaandroid

提问by Emmanuel Guther

Hi I have a listview showing an arraylist, when an item is clicked it is added to another arraylist and displayed in another activity. What is my problem? I want that if an item (eg a dog) I touch once, is added to the second activity, and it shows. But if I were to touch the item (dog) is not added again.

嗨,我有一个显示数组列表的列表视图,当单击一个项目时,它会被添加到另一个数组列表并显示在另一个活动中。我的问题是什么?我希望如果我触摸一次的项目(例如狗)被添加到第二个活动中,并且它会显示。但是,如果我要触摸该项目(狗),则不会再次添加。

We would say that I want to check if it exists, not add.

我们会说我想检查它是否存在,而不是添加。

I've tried so, but without success.

我已经尝试过,但没有成功。

if (page2 == null)
{
    page2 = (ListView) LayoutInflater.from(Local_mostrar_bebidaActivity.this).inflate(R.layout.page_two_viewpager_listview, null);
    page2.setAdapter(adapterlistvMenuVINOSespumosos);
    page2.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
            if (MiPedidoActivity.itemsListVMiPedido.contains( position)){}
            else
                MiPedidoActivity.itemsListVMiPedido.add(itemsListVMenuVINOSespumosos.get(position));

        }});
    }
    page = page2;
    break;

Any ideas?

有任何想法吗?

采纳答案by Jason C

You have:

你有:

if (MiPedidoActivity.itemsListVMiPedido.contains( position)){}
    else
MiPedidoActivity.itemsListVMiPedido.add(itemsListVMenuVINOSespumosos.get(position));

You are checking if itemsListVMiPedidocontains the integer, position, but you are adding itemsListVMenuVINOSespumosos.get(position)to the list. You need to check if the list contains them item, not the index (think of it exactly like what you are trying to do: "do not add item to list if list already contains item). You probably mean something like this (I just made up Itemfor example, use whatever class your objects are):

您正在检查是否itemsListVMiPedido包含整数, position,但您正在添加itemsListVMenuVINOSespumosos.get(position)到列表中。您需要检查列表是否包含它们item,而不是索引(把它想象成你正在尝试做的事情:“如果列表已经包含item ,则不要将 item 添加到列表中)。您的意思可能是这样的(我只是由Item例如,使用任何类的对象):

Item item = itemsListVMenuVINOSespumosos.get(position);
if (MiPedidoActivity.itemsListVMiPedido.contains(item)) { // <- look for item!
   // ... item already in list
} else {
   MiPedidoActivity.itemsListVMiPedido.add(item);
}

By the way, as a suggestion, if your item classes have equals()and hashCode()properly implemented, consider a LinkedHashSet(which will preserve insertion order but will not allow duplicates). There are other Setimplementations that may be useful too (e.g. TreeSetif your items implement Comparable), depending on your ordering/sorting requirements.

顺便说,作为一个建议,如果您的项目类都equals()hashCode()正确实施,考虑LinkedHashSet(将保留插入顺序,但不会允许重复)。根据您的排序/排序要求,还有其他Set可能有用的实现(例如,TreeSet如果您的项目实现Comparable)。