Recyclerview notifyDataSetChanged();不工作

时间:2017-08-07 12:26:06

标签: android android-recyclerview

我尝试从RecyclerView中删除项目,然后使用以下代码再次更新列表

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

此处回收器在删除项目后未更新。 如果我回去,只来更新列表。我也试过OnItemClickListener从活动中删除。  notifyDataSetChanged();我试过这个,我也试过notifyItemRemoved(position);但没有工作 有人建议这样做吗??

3 个答案:

答案 0 :(得分:4)

因此您需要更新代码。根据您的deleteCode方法,您的mList不会在删除时被修改。 notifyDataSetChanged仅在填充适配器的基础列表发生更改时才有效。

因此,您需要在致电mList

之前更新notifyDataSetChanged

所以将代码修改为 -

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

});

答案 1 :(得分:2)

var query = (from A in ctx.Persons
             select new {
             AverageAge = ctx.Persons.Average(p => (Convert.ToInt32(DateTime.Now.ToString("yyyy")) - Convert.ToInt32(p.BirthDay)))
             }).Distinct();

答案 2 :(得分:0)

List<CodeItem> mList致电之前,您的数据notifyDataSetChanged未被修改。在notifyDataSetChanged致电之前从列表中删除专有项目。