Reputation: 1162
I am new to recyclerView. I am creating an app which in which when i send my data to firebase
database, a image view with check mark with my data in recyclerView
comes in stating that my data has reached database.
I did this by creating a child key which sets value from processing to reached when data has reached the servers.
I also created a imageView
which sets its resource as check mark if message has reached.
Till now it works fine. But the issue is that my recyclerView doesn't refresh on the same moment.
If I press Back Button (Even Close The Activity) and then come back to same activity, image view is now there . Hence, you can understand that my task done till now is working properly. I even know that by adding listener to my recyclerView can handle this issue.
FirebaseDatabase.getInstance().getReference().child("message").push().setValue(newMessage, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
databaseReference.child("reachedServer").setValue("reached");
}
});
But I have tried every thing to refresh my recyclerView on child change.
But just it does not work.I have now erased my commands when child changes in firebase
.
Please help me so that I can refresh my recyclerView
when child changes.
PS. I store my data in my custom class and I also use custom adapter.
Upvotes: 1
Views: 6735
Reputation: 379
To refresh the adapter only for the item changed use the following code on your onChildChaged() method:
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
for (int i = 0; i < messagesList.size(); i++) {
if (messagesList.get(i).getMessageID().equals(message.getMessageID())) {
messagesList.set(i, message);
adapter.notifyItemChanged(i);
break;
}
}
}
Make sure you have a unique message ID for each message and its getter in your Message model class, in the example above this was called MessageID and the getter getMessageID().
Upvotes: -1
Reputation: 228
try this
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
recycler_view.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
Upvotes: 0
Reputation: 1162
Yay, I did it.
I did this In OnChildChanged Method Of childEventListener of my database reference.
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Message newMessage = dataSnapshot.getValue(Message.class);
if (newMessage != null) {
for (int i = 0; i < myArrayList().size(); i++) {
if (myArrayList().get(i).timestamp == newMessage.timestamp) {
myArrayList().remove(i);
myArrayList().add(i, newMessage);
break;
}
}
}
adapter.notifyDataSetChanged();
}
}
Upvotes: 2
Reputation: 80942
To refresh the adapter you can use:
adapter.notifyDataSetChanged();
notifyDataSetChanged
void notifyDataSetChanged ()
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
more info here: https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()
Upvotes: 3
Reputation: 2937
Your CompletionListener
is triggered when data is already inserted, it means you do not need any additional changes to your database in order to display something on your client device(the same one, which was making changes).
Simply: inside CompletionListener
update specific recycleView record. You can do this with notifyItemChanged(position)
or notifyItemChanged(position, payload)
,
considering 1st call you have to change data record which is used inside onBindViewHolder
in your adapter, or you can set a flag to that adapter which will be used in onBindViewHolder
. 2nd call gives you availability to pass any additional data.
Keep in mind that notifyItemChanged(position, payload)
call is 1st and it has notifyItemChanged(position)
call inside it. Do you need any additional info?
Upvotes: -1