My Code
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) {
}
});
}
}
The things I've tried
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();
My activity just gets user data from firebase and presently it has 2 childs
if I change the data of an user the data in the arraylist should be deleted and added again to the changed data
Suppose the value of User A is X
the value of User B is Y
It shows that way in activity and if I change the value of User A to P from X
I want the activity to show the new data which is P
But in the methods that I tried.
1st One Im getting 3 values which is A = X, B = Y and A = P
As you can see new data is added but existing data isn't removed
In the 2nd Method the activity just keeps refreshing with only ne data which is A = P
When new data is added I want to delete the old data and show the new one without repetition
The methods I tried, The code was inside the onDataChange
Helper Class
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;
}
}