如何检查数据库中的元素是否存在于数组listview android中

时间:2012-07-12 13:46:52

标签: android listview arraylist

我有一个数组列表。我需要经历它,看看输入中是否有一个元素。怎么做?

DatabaseUtil dbUtil1 = new DatabaseUtil(this);
        dbUtil1.open();
        Boolean contain=false;
        Cursor cursor = dbUtil1.fetchAllElements();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Model s= new Model(cursor.getString(1),cursor.getString(2));


                if (list.contains(s)) //THIS DOES NOT WORK
                {
                    contain=true;
                }
                else 
                    {
                    list.add(get(cursor.getString(1),cursor.getString(2)));
                    }

            }
        }

        dbUtil1.close();    

1 个答案:

答案 0 :(得分:0)

你的实例是一个新对象.... 像这样在你的其他地方添加你创建的对象:

            Model s= new Model(cursor.getString(1),cursor.getString(2));


            if (list.contains(s)) //THIS DOES NOT WORK
            {
                contain=true;
            }
            else 
            {
                list.add(s);
            }