1

I have a Fragment inside MainActivty, and inside this Fragment RecyclerView of Articles with cardView (inside this card i have like button and like counter) and when the user click on the cardView will open DetailsActivity that

and I want when onBackPressed to update just this item depends on the data changed (like button and like counter) in DetailsActivity

how to update just this Item ?

sorry for bad English!

abdu
  • 301
  • 1
  • 2
  • 16

1 Answers1

3

First update the data of that specific position item of adapter.

After that call notifyItemChanged(int) to update that specific CardView of RecyclerView

Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);

This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • Ok i sent back (via OnBackPressed) a position and the object model of this item from DetailsActivity to MainActivity (that holds a Fragment that holds a RecyclerView). and received it (via OnActivityResult) in MainActivity. the question here is how to send this position and object model from MainActivity to the fragment to perform notifyItemChanged(position, payload) – abdu Nov 09 '16 at 10:23
  • Create one method in fragment for update item and call it from `MainActivity` when you received in `OnActivityResult` – Priyank Patel Nov 09 '16 at 10:27
  • i added this method in fragment and i called it from MainAnctivity public void updateItem(int position) { if (myAdapter != null) { myAdapter.notifyItemChanged(position); } } and I get myAdapter null !! – abdu Nov 09 '16 at 10:57
  • Post your full code in question. So, I can correct it. – Priyank Patel Nov 09 '16 at 12:06
  • @Priyank Patel So I have a Recycler. list of CardViews from a user entering data on a UI form. A click on a CardView brings the user to a single CardView Activity to show that CardView's details. A click on that CardView launches an Edit Activity so user can edit the original data. How do I update the View for the single CardView Activity view when the user clicks "Save" button in my Edit Activity? I use OnActivityResult() in CardView Activity with the Edit Activity so the user is returned to the single CardView Activity but the old View and data is showing. – AJW Aug 24 '17 at 20:38
  • @AJW you need to pass updated data in intent to `OnActivityResult()` from edit activity to single card view activity. – Priyank Patel Aug 25 '17 at 10:05
  • @Priyank Patel I try that but it does not work. Is that because the onCreate() in single CardView Activity gets item object and data from the MainActivity (the RecyclerView list)? – AJW Aug 25 '17 at 15:31
  • @AJW Can you create another question with your problem code? So that, I can understand your issue better and answer it. – Priyank Patel Aug 28 '17 at 07:09