I have tried to delete the items from RecyclerView and update the list again using below code
public class ScannedCodesAdapter extends RecyclerView.Adapter<ScannedCodesAdapter.ViewHolder> {
private List<CodeItem> mList;
private Context mContext;
private OnItemClickListener mClick;
public ScannedCodesAdapter(List<CodeItem> list, Context context, OnItemClickListener click) {
mList = list;
mContext = context;
mClick = click;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ScanProductCodeItemBinding mBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.scan_product_code_item, parent, false);
return new ViewHolder(mBinding);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
CodeItem item = mList.get(position);
String code = item.getCode();
holder.binding.adapterCode.setText(code);
int num = position + 1;
if (mList != null)
holder.binding.adapterNum.setText(num + " ");
if ((position % 2) == 0) {
holder.itemView.setBackgroundResource(R.color.white);
} else {
holder.itemView.setBackgroundResource(R.color.color2);
}
if (item.selected) {
holder.binding.adapterDelete.setVisibility(View.VISIBLE);
} else {
holder.binding.adapterDelete.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(view -> {
for (CodeItem m : mList) {
m.selected = false;
}
item.selected = true;
notifyDataSetChanged();
});
holder.binding.adapterDelete.setOnClickListener(view ->
{
// mClick.onClick(view, position);
//notifyItemRemoved(position);
SQLiteDataBaseHandler handler = new SQLiteDataBaseHandler(mContext);
CodeItem codeItem = handler.getCodeItem(code);
if (item != null) {
handler.deleteCode(codeItem);
notifyDataSetChanged();
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ScanProductCodeItemBinding binding;
public ViewHolder(ScanProductCodeItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
in here recycler not updating after delete the item. If I go back and come only update the list. also I tried OnItemClickListener to remove from the activity. notifyDataSetChanged(); I tried this and also I tried notifyItemRemoved(position); but not working Anyone suggest the idea to do this ??
So you need to update your code. Based on your deleteCode
method your mList is not getting modified on delete. notifyDataSetChanged
only works if the underlying list that populates the adapter changes.
So you need to update your mList
before calling notifyDataSetChanged
So modify your code as -
holder.binding.adapterDelete.setOnClickListener(view ->
{
//Remove from list
mList.remove(position);
notifyDataSetChanged();
//...delete from db ...
SQLiteDataBaseHandler handler = new SQLiteDataBaseHandler(mContext);
CodeItem codeItem = handler.getCodeItem(code);
if (item != null) {
handler.deleteCode(codeItem);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With