所以有一个recyclerViewAdapter填充来自RoomDB的数据并显示它。 当用户长按每一行并选择删除时,我想执行删除查询。 我应该如何检索所选行的数据(例如名称或id)并在OnContextItemSelected的MainActivity中接收它,以便我可以从我的Db中删除它? -Thanks
private StudentViewHolder(View itemView) {
super(itemView);
nameItemView = itemView.findViewById(R.id.nameTextView);
numberItemView = itemView.findViewById(R.id.numberTextView);
imageItemView = itemView.findViewById(R.id.profile_image);
idItemView = itemView.findViewById(R.id.idView);
itemView.setOnCreateContextMenuListener(this); }
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
menu.setHeaderTitle("Select Action");
menu.add(Menu.NONE,0,getAdapterPosition(),"Delete");
menu.add(Menu.NONE,1,getAdapterPosition(),"Update");
}
MainActivity.java 我尝试了很多东西,却找不到线索。
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();????
Log.e(,"info ="+ info);
Student student = mStudentViewModel.getAllStudents().getValue().get(item.getGroupId());?????
Student student1 = adapter.getClass().geti?????
switch (item.getGroupId()){?????
case 0 :
break;
case 1:
break;
}
return true;
}
答案 0 :(得分:0)
当用户通过覆盖onSwiped滑动recyclerView时,我设法执行了删除查询!
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder,
int direction) {
int position = viewHolder.getAdapterPosition();
S s;
s= adapterAll.getSAtPosition(position);
// Delete
mStudentViewModel.delete(s);
}
});