我試圖再次刪除RecyclerView的項目和更新列表這裏回收不更新刪除的項目後使用下面的代碼Recyclerview notifyDataSetChanged();不工作
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);但不工作 任何人都建議這樣做的想法??
貴handler.deleteCode(codeItem);更新mList?你可以添加deleteCode方法的代碼嗎? –
public void deleteCode(CodeItem codeItem){ SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CODE,KEY_CODE +「=?」, new String [] {String.valueOf(codeItem.getStatus())}); db.close(); } 這是刪除項目的代碼 – AngelJanniee
請參閱下面的答案。因爲你的mList沒有改變,你的notifyDataSet將不起作用。爲了使它工作,填充適配器的列表應該改變。 –