帮助程序类
public class HomeScreenChatsHelper implements Comparable {
private String ID;
private String Name;
private String Image;
private String From;
private String Seen;
private String LastMessage;
private String LastMessageTime;
public HomeScreenChatsHelper(){
}
public HomeScreenChatsHelper(String id, String name, String image, String from, String seen, String lastmessage, String lastMessageTime) {
this.ID=id;
this.Name = name;
this.Image = image;
this.From = from;
this.Seen = seen;
this.LastMessage = lastmessage;
this.LastMessageTime = lastMessageTime;
}
public String getID() {
return ID;
}
public void setID(String id) {
ID = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getMessage() {
return LastMessage;
}
public void setMessage(String message) {
LastMessage = message;
}
public String getTime() {
return LastMessageTime;
}
public void setTime(String time) {
LastMessageTime = time;
}
public String getFrom() {
return From;
}
public void setFrom(String from) {
From = from;
}
public String getSeen() {
return Seen;
}
public void setSeen(String seen) {
Seen = seen;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public int compareTo(Object comparestu) {
long compareage= Long.parseLong(((HomeScreenChatsHelper)comparestu).getTime());
long a = Long.parseLong(LastMessageTime);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
}
return Long.compare(a,compareage);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HomeScreenChatsHelper)) return false;
HomeScreenChatsHelper that = (HomeScreenChatsHelper) o;
return getID().equals(that.getID());
}
@Override
public int hashCode() {
return getID().hashCode();
}
活动
for(HomeScreenChatsHelper str : mChats) {
if (str.getID().equals(ID)) {
mChats.remove(ID);
break;
}
}
关于如何做到这一点,有很多教程,而过去一周我一直在寻找解决方案,但我仍然没有。无论如何,仅指定ID即可删除整个对象吗?我将没有其他所有字段的值,因此我只想通过其ID删除特定对象。我也不能使用clear选项,因为我需要其他数据。那有人可以帮我吗?
使用当前代码没有任何反应。没有错误,但不起作用
答案 0 :(得分:0)
通过使用java-8,您可以过滤列表,结果将是List<HomeScreenChatsHelper>
与HomeScreenChatsHelper
相同的id
List<HomeScreenChatsHelper> mChats = new ArrayList<>();
//filter
List<HomeScreenChatsHelper> result = mChats.stream()
.filter(str->!str.getId().equals(Id)).
.collect(Collectors.toList());
或使用Iterator
// Iterator.remove()
Iterator itr = mChats.iterator();
while (itr.hasNext())
{
HomeScreenChatsHelper x = itr.next();
if (x.getId().equals(Id)) }
itr.remove();
}
}
答案 1 :(得分:0)
您的问题还不清楚。 mChats
是包含HomeScreenChatsHelper
对象的列表吗?
我想是这样。如果是这种情况,则可以将foreach
循环更改为普通循环
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++){
if (mChats.get(i).getID().equals(ID)) {
mChats.remove(i);
break;
}
}
答案 2 :(得分:0)
Java 8或更高版本中最简单的方法是使用Collection#removeIf
:
mChats.removeIf(str -> str.getID().equals(ID));
顺便说一句,Java中的约定是字段以小写字母开头。