添加前删除Arraylist数据

时间:2019-02-14 11:29:51

标签: java android database firebase arraylist

我的代码

 private void addOnlineData(final String item) {
    mChildEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null) {

                final String message = dataSnapshot.child("Message").getValue().toString();
                final String time = dataSnapshot.child("Time").getValue().toString();
                final String from = dataSnapshot.child("From").getValue().toString();
                final String seen = dataSnapshot.child("Seen").getValue().toString();

                LastSeen lastSeeen = new LastSeen();
                long lastTime = Long.parseLong(time);
                final String lastSeenTime = lastSeeen.lastSeen(lastTime, getActivity());

                mDatabaseReference.child("UserData").child(item).child("Name").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String name = dataSnapshot.getValue().toString();

//CODE GIVEN BELOW WAS HERE

                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        }

我尝试过的事情

1

                        Log.d("HHHHH2222", String.valueOf(mChats.size()));
  mChats.remove(new HomeScreenChatsHelper(name, message, lastSeenTime, from, seen));
                        Log.d("HHHHH2222", String.valueOf(mChats.size()));
  mChats.add(new HomeScreenChatsHelper(name, message, lastSeenTime, from, seen));
                        mAdapter.notifyDataSetChanged();

2

                        Log.d("HHHHH2222", String.valueOf(mChats.size()));
                        mChats.clear();
                        mChats.add(new HomeScreenChatsHelper(name, message, lastSeenTime, from, seen));
                        Log.d("HHHHH2222", String.valueOf(mChats.size()));
                        mAdapter.notifyDataSetChanged();

我的活动刚刚从firebase获取用户数据,目前有2个孩子
如果我更改用户的数据,则应该删除arraylist中的数据,然后再次将其添加到更改的数据中

假设用户A的值为X
        用户B的值为Y

它显示了活动中的这种方式,并且如果我将用户A的值从X更改为P,
我希望活动显示新数据P

但是我尝试过的方法。

第一个Im获得3个值,即A = X,B = Y和A = P
如您所见,新数据已添加,但现有数据未删除

在第二种方法中,活动仅使用ne = A = P的数据不断刷新

添加新数据后,我想删除旧数据,并重复显示新数据

我尝试过的方法,代码在onDataChange内部

助手类

public class HomeScreenChatsHelper {

private String Name;
private String LastMessage;
private String LastMessageTime;
private String From;
private String Seen;

public HomeScreenChatsHelper(){
}

public HomeScreenChatsHelper(String name, String lastMessage, String lastMessageTime, String from, String seen) {
    this.Name = name;
    this.LastMessage = lastMessage;
    this.LastMessageTime = lastMessageTime;
    this.From = from;
    this.Seen = seen;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

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;
}

}

3 个答案:

答案 0 :(得分:0)

根据我的见解,您的第一种方法尝试删除A = P,然后添加A =P。

如果您管理如何将mChats.remove(object)中的对象更改为A =X。应该可以。

只需覆盖所有内容mChats.clear();使mChats为空。

答案 1 :(得分:0)

您的HomeScreenChatsHelper很可能缺少hashCode()(或equals()和类似方法)运行所需的List#remove(Object)List#contains(Object)方法,因此它知道列表中的哪个HomeScreenChatsHelper与您要删除的那个相同。

大多数IDE将具有自动生成这两种方法的方法。在IntelliJ IDE中,按Alt +插入(或右键单击>生成...)

IntelliJ Generate Menu

然后单击“ equals()和hashCode()”

或者在Eclipse中,右键单击源上的某个位置,然后选择: “源”>“生成hashCode()和equals()...”

Eclipse Right Click Menu

无论哪种方式,它都会为您的班级产生类似以下的内容:

public class HomeScreenChatsHelper {

    private String name;
    private String lastMessage;
    private String lastMessageTime;
    private String from;
    private String seen;

    @Override
    public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((from == null) ? 0 : from.hashCode());
      result = prime * result + ((lastMessage == null) ? 0 : lastMessage.hashCode());
      result = prime * result + ((lastMessageTime == null) ? 0 : lastMessageTime.hashCode());
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      result = prime * result + ((seen == null) ? 0 : seen.hashCode());
      return result;
    }
    @Override
    public boolean equals(Object obj) {
      if (this == obj)
        return true;
      if (obj == null)
        return false;
      if (getClass() != obj.getClass())
        return false;
      HomeScreenChatsHelper other = (HomeScreenChatsHelper) obj;
      if (from == null) {
        if (other.from != null)
          return false;
      } else if (!from.equals(other.from))
        return false;
      if (lastMessage == null) {
        if (other.lastMessage != null)
          return false;
      } else if (!lastMessage.equals(other.lastMessage))
        return false;
      if (lastMessageTime == null) {
        if (other.lastMessageTime != null)
          return false;
      } else if (!lastMessageTime.equals(other.lastMessageTime))
        return false;
      if (name == null) {
        if (other.name != null)
          return false;
      } else if (!name.equals(other.name))
        return false;
      if (seen == null) {
        if (other.seen != null)
          return false;
      } else if (!seen.equals(other.seen))
        return false;
      return true;
    }

    // Getters & Setters
}

通过此操作,您的选项1 应该可以按预期运行,方法是删除带有旧数据的HomeScreenChatsHelper,然后添加带有新数据的HomeScreenChatsHelper(在当前选项1的代码,您既要删除也要添加新数据。)

答案 2 :(得分:0)

您可以做的是保留一个单独的HashMap user_names.put(user_code, user_name),并在该HashMap user_names.replace(user_code, user_name)中更新名称

注意:仅在不存在时添加用户,否则进行更新。

在语句中传递HashMap user_key

mChats.add(new HomeScreenChatsHelper(user_names.user_key, message, lastSeenTime, from, seen));

要显示用户名时,可以从user_names HashMap中获取它。

user_names.get(user_key)