After add several crimes and deleting crime(except the last one) app get a crash
Please show your CrimeListFragment.java
private void updateUI() {
CrimeLab crimeLab = CrimeLab.getInstance(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
if (mCrimeAdapter == null) {
mCrimeAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mCrimeAdapter);
} else {
mCrimeAdapter.setCrimes(crimes); // Please check whether you update the data of the adapter
mCrimeAdapter.notifyDataSetChanged();
}
// ......
}
Thanks for your reminder.
I hava find the reason and fixed it; when deleting the item, need call notifyItemRemoved not notifyItemChanged;
private void updateUI(){
Log.d(TAG, “update ui”+mActionType);
CrimeLab crimeLab = CrimeLab.get(getActivity());
List crimes = crimeLab.getCrimes();
if(mAdapter == null){
mAdapter = new CrimeAdapter(crimes);
mCrimeRecycleView.setAdapter(mAdapter);
}else{
if(mLastAdapterClickPosition < 0){
mAdapter.notifyDataSetChanged();
}else{
Log.d(TAG, “notifyItemChanged—”+mLastAdapterClickPosition);
if(mActionType == “delete”){
mAdapter.notifyItemRemoved(mLastAdapterClickPosition);
mActionType = “”;
}else{
mAdapter.notifyItemChanged(mLastAdapterClickPosition);
}
mLastAdapterClickPosition = -1;
}
}
updateSubtitle();
}