I am working with ArrayList ,Here I want to remove objects with similar values from Arraylist. I tried many solutions posted over stackoverflow but something is wrong in my code due to which code is not working . I am getting list with dupliactes .
Here is my code :
public class StateCityModel {
private String id ;
private String code ;
private String name ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof StateCityModel))
return false;
return id.equals(((StateCityModel) obj).getId());
}
@Override
public int hashCode() {
return (id == null) ? 0 : id.hashCode();
}
}
Code add values in the ArrayList
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
businessTypeObj = clubsList.get(position);
selectedBusinessTypeList.add(businessTypeObj);
}
}
});
Code to remove objects with similar values .
Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);
After doing above code I am getting objects with similar values in the selectedBusinessTypeList .
Help me please , I am not able to find what is wrong in the above code .
You can also do in this way
Step 1: Only insert those data in arraylist which id's are not same so after that you don't need to remove duplicate elements from arraylist.
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
businessTypeObj = clubsList.get(position);
selectedBusinessTypeList.add(businessTypeObj);
}
}
});
Replace it by
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
}
StateCityModel stateCityModel = new StateCityModel();
stateCityModel = clubsList.get(position);;
if (!selectedBusinessTypeList.contains(stateCityModel)){
selectedBusinessTypeList.add(stateCityModel);
}
}
});
Step2 : Remove this not needed
Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);
I guess you are seeing duplicate values in the list right ? Since reference of selectedBusinessTypeList is changed. Change adapter also by creating new adapter by passing new selectedBusinessTypeList and set that adapter to listview again inside that onClick
You can replace the code to add values by below lines:
businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
businessTypeObj = clubsList.get(position);
if (!selectedBusinessTypeList.contains(businessTypeObj)) {
selectedBusinessTypeList.add(businessTypeObj);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.