2017-08-07 130 views
4

我試圖再次刪除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);但不工作 任何人都建議這樣做的想法??

+0

貴handler.deleteCode(codeItem);更新mList?你可以添加deleteCode方法的代碼嗎? –

+0

public void deleteCode(CodeItem codeItem){ SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CODE,KEY_CODE +「=?」, new String [] {String.valueOf(codeItem.getStatus())}); db.close(); } 這是刪除項目的代碼 – AngelJanniee

+0

請參閱下面的答案。因爲你的mList沒有改變,你的notifyDataSet將不起作用。爲了使它工作,填充適配器的列表應該改變。 –

回答

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

}); 
+0

是的,謝謝@kapsym,在這種情況下,我沒有刪除列表中的項目。 – AngelJanniee

+1

@AngelJanniee很棒。樂於幫助 :) –

0

您的數據List<CodeItem> mList在撥打notifyDataSetChanged之前未被修改。撥打notifyDataSetChanged之前,從列表中刪除專用項目。

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

    }); 
相關問題