0

I have a RecyclerView in MainActivity that loads saved data from database using Android Architecture Components (Room), this data is then passed to ChildActivity via intent as a parcellable bundle. I wish to be able to delete this data upon button click from ChildActivity: the issue I have now is to get the RecyclerView holder position in order to delete this particular part of the RecyclerView data. I am aware this is easier by swipe to delete from MainActivity, but I don't want to do that since I will be deleting from ChildActivity upon button click from the context. I have come up with the below method, but it's not just working;

public void deleteMovie( RecyclerView.ViewHolder viewHolder){ 
    int position = viewHolder.getAdapterPosition();
    List<DataObject> movie = mAdapter.getMovieList();
    mDb.dataDao().deleteData(movie.get(position);
}

I don't know where to go from here. This method works well for overriden onSwipe method of itemTouchHelper class, but this is not my intention. Kindly help bail me out.

MrVasilev
  • 1,503
  • 2
  • 17
  • 34
  • refer this may be use full https://stackoverflow.com/questions/46494987/remove-recyclerview-adapter-item-from-activity – sasikumar Sep 05 '18 at 05:26

2 Answers2

0

After deleting the data refresh your adapter

 public void deleteMovie( RecyclerView.ViewHolder viewHolder){
        int position = viewHolder.getAdapterPosition();
        List<DataObject> movie = mAdapter.getMovieList();
        mDb.dataDao().deleteData(movie.get(position);
        mAdapter.notifydatasetchanged()
    }
Rohan Pawar
  • 1,875
  • 22
  • 40
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

I guess this data which is passed to ChildActivity contains the IDof the record in the Database's table? So, you can delete this record by ID or what you use for @PrimaryKey

If this suggestion not helps, please provide more code for better understanding

MrVasilev
  • 1,503
  • 2
  • 17
  • 34