5

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 ??

3
  • Does your handler.deleteCode(codeItem); update the mList? Can you add the code for deleteCode method. Commented Aug 7, 2017 at 12:29
  • public void deleteCode(CodeItem codeItem) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CODE, KEY_CODE + " = ?", new String[]{String.valueOf(codeItem.getStatus())}); db.close(); } This is the code for delete the item Commented Aug 7, 2017 at 12:31
  • See my answer below. since your mList is not changing your notifyDataSet will not work. For it to work your list that populates the adapter should change. Commented Aug 7, 2017 at 12:35

3 Answers 3

5

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); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah thanks @kapsym, I didn't remove the item in the list in this case.
@AngelJanniee great. Happy to help :)
2
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); }); 

Comments

0

Your data List<CodeItem> mList isn't modified before notifyDataSetChanged call. Remove appropriative item from the list before notifyDataSetChanged call.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.