1

我有一个arrayList,我想知道新添加的项目是否已经在列表中。

    protected void onPostExecute(Void result1) {
            //TODO Auto-generated method stub
            super.onPostExecute(result1);
            progressBar.dismiss();

            if(status == 1) {   
                //arrayListItemEntries.add(itemEntryAdded);
                System.out.println(itemEntryAdded);
                System.out.println(arrayListItemEntries.get(arrayListItemEntries.size()-1));
                arrayListNewItems.add(itemEntryAdded);
                adapterItem.notifyDataSetChanged();
                toastMsg = "Item " + description +" is added on the list";                  
            }
            else
            {
                toastMsg ="Item not Found";
            }
            makeToast(toastMsg);
        }

    }

在线上,

 System.out.println(arrayListItemEntries.get(arrayListItemEntries.size()-1));

它输出null,为什么?当我想输出列表大小时,它是正确的。

4

3 回答 3

1

尝试这样的事情: -

if(arrayListItemEntries.contains(itemEntryAdded))
        // do nothing - already there in list
else
        arrayListItemEntries.add(itemEntryAdded);
于 2013-02-19T08:19:40.803 回答
0

您的列表大小是多少?你在哪里添加项目到这个列表?

通常我们使用.contains(Object)方法来检查特定对象的存在。

您也可以尝试使用Set不允许重复的,因此您不需要检查。

于 2013-02-19T08:23:29.590 回答
0

只需使用 contains(item)方法。

于 2013-02-19T08:20:50.127 回答