Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview notifyDataSetChanged(); not working

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

like image 560
AngelJanniee Avatar asked Aug 07 '17 12:08

AngelJanniee


1 Answers

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);

});
like image 94
Kapil G Avatar answered Nov 14 '22 22:11

Kapil G