Just want to check if data already exist(s) or not in ArrayList, but everytime getting : Not Exist(s) whereas data exists in ArrayList, and as a result i am getting duplicate records in a list..
只想检查数据是否已存在于ArrayList中,但每次都得到:Not Exist(s)而数据存在于ArrayList中,因此我在列表中获得重复记录。
DataArrayList.secondArraylist.add(new Second(actorList.get(position).getName(), actorList.get(position).getImage()));
System.out.println(DataArrayList.secondArraylist.size());
String strName = actorList.get(position).getName().toString();
Log.d("name:", strName);
for(int i=0; i<DataArrayList.secondArraylist.size(); i++)
{
if(DataArrayList.secondArraylist.get(i).getName().equals(strName)) {
System.out.println(DataArrayList.secondArraylist.get(i).getName());
Toast.makeText(context, "Exist(s)", Toast.LENGTH_SHORT);
}
else {
Toast.makeText(context, "Not Exist(s)", Toast.LENGTH_SHORT);
}
}
Problem:
Not getting any Toast message, which i am using to indicate that "Data Exists" or "Not"
没有得到任何Toast消息,我用它来表示“数据存在”或“不存在”
Finally:
I would like to add item to arraylist if already not exist(s), else want to show Toast that item already exist(s)
我想将项目添加到arraylist如果已经不存在,否则想要显示Toast该项目已经存在(s)
4 个解决方案
#1
You are comparing your list object DataArrayList.secondArraylist
with a String
您正在将列表对象DataArrayList.secondArraylist与String进行比较
if (DataArrayList.secondArraylist.equals(strName)) {
That will always return false
. Instead, create a method that loops through the list and checks the strName
against the name of the Second
objects that are stored in the list.
那将永远是假的。而是创建一个循环遍历列表的方法,并根据列表中存储的第二个对象的名称检查strName。
boolean contains(ArrayList<Second> list, String name) {
for (Second item : list) {
if (item.getName().equals(name)) {
return true;
}
}
return false;
}
Then use it
然后使用它
if (contains(DataArrayList.secondArraylist, strName)) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#2
The contains method calls the equals method with signature equals(Object), so you requires to override this method in your POJO class.
contains方法使用签名equals(Object)调用equals方法,因此您需要在POJO类中重写此方法。
If you override equals(Object) then you should also override hashcode()
如果你重写equals(Object),那么你也应该覆盖hashcode()
Code snippet :
代码段:
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object obj) {
return !super.equals(obj);
}
@Override
public int hashCode() {
return getName().hashCode();
}
Edit :
Moreover, The contains method is used for Object. So, your condition should look like this :
而且,contains方法用于Object。所以,你的情况应该是这样的:
(DataArrayList.secondArraylist.contains(actorList.get(position)))
Final code :
最终代码:
if (DataArrayList.secondArraylist.contains(actorList.get(position))) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#3
You need to use indexOf()
while checking a value in ArrayList
您需要在检查ArrayList中的值时使用indexOf()
if (DataArrayList.secondArraylist.indexOf(strName)!=-1) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#4
As the strName
you are checking is a property of your Object Second
, you cannot check it directly using method of the ArrayList
. The easiest way would be loop through the ArrayList DataArrayList.secondArraylist
, get each Second
object contained in the list, then check if strName
is equal to that property of your Second
object.
由于您要检查的strName是Object Second的属性,因此无法使用ArrayList的方法直接检查它。最简单的方法是遍历ArrayList DataArrayList.secondArraylist,获取列表中包含的每个Second对象,然后检查strName是否等于Second对象的该属性。
Not sure how you implement the Second
object, but you should get the corresponding String from each Second
object then compare it with the strName
.
不确定如何实现Second对象,但是你应该从每个Second对象获取相应的String,然后将它与strName进行比较。
#1
You are comparing your list object DataArrayList.secondArraylist
with a String
您正在将列表对象DataArrayList.secondArraylist与String进行比较
if (DataArrayList.secondArraylist.equals(strName)) {
That will always return false
. Instead, create a method that loops through the list and checks the strName
against the name of the Second
objects that are stored in the list.
那将永远是假的。而是创建一个循环遍历列表的方法,并根据列表中存储的第二个对象的名称检查strName。
boolean contains(ArrayList<Second> list, String name) {
for (Second item : list) {
if (item.getName().equals(name)) {
return true;
}
}
return false;
}
Then use it
然后使用它
if (contains(DataArrayList.secondArraylist, strName)) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#2
The contains method calls the equals method with signature equals(Object), so you requires to override this method in your POJO class.
contains方法使用签名equals(Object)调用equals方法,因此您需要在POJO类中重写此方法。
If you override equals(Object) then you should also override hashcode()
如果你重写equals(Object),那么你也应该覆盖hashcode()
Code snippet :
代码段:
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object obj) {
return !super.equals(obj);
}
@Override
public int hashCode() {
return getName().hashCode();
}
Edit :
Moreover, The contains method is used for Object. So, your condition should look like this :
而且,contains方法用于Object。所以,你的情况应该是这样的:
(DataArrayList.secondArraylist.contains(actorList.get(position)))
Final code :
最终代码:
if (DataArrayList.secondArraylist.contains(actorList.get(position))) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#3
You need to use indexOf()
while checking a value in ArrayList
您需要在检查ArrayList中的值时使用indexOf()
if (DataArrayList.secondArraylist.indexOf(strName)!=-1) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
#4
As the strName
you are checking is a property of your Object Second
, you cannot check it directly using method of the ArrayList
. The easiest way would be loop through the ArrayList DataArrayList.secondArraylist
, get each Second
object contained in the list, then check if strName
is equal to that property of your Second
object.
由于您要检查的strName是Object Second的属性,因此无法使用ArrayList的方法直接检查它。最简单的方法是遍历ArrayList DataArrayList.secondArraylist,获取列表中包含的每个Second对象,然后检查strName是否等于Second对象的该属性。
Not sure how you implement the Second
object, but you should get the corresponding String from each Second
object then compare it with the strName
.
不确定如何实现Second对象,但是你应该从每个Second对象获取相应的String,然后将它与strName进行比较。