我有RecyclerView
绑定杂货项目。添加项目非常有效。但是,当我尝试删除该项时,应用程序崩溃,我收到IndexOutOfBoundsException
错误。
我面临的问题出在onBindViewHolder()
。我试图获得适配器位置,但没有解决问题。我也尝试使用notifyDataSetChanged()
并删除了我的整个列表。
我的问题是我做错了什么以及如何解决这个问题?
这是Adapter类:
private class GroceryAdapter extends RecyclerView.Adapter<GroceryHolder>{
private List<Grocery> groceries;
private GroceryHolder holder;
public GroceryAdapter(){
setGroceries(GroceryList.get(GroceryActivity.this).getGroceries());
}
public void setGroceries(List<Grocery> groceries){
this.groceries = groceries;
}
@Override
public GroceryHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutInflater= LayoutInflater.from(GroceryActivity.this);
View view=layoutInflater.inflate(R.layout.grocery_list_row, parent, false);
holder =new GroceryHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final GroceryHolder holder, final int position){
final Grocery grocery = groceries.get(position);
holder.bindGrocery(grocery);
//private Grocery grocery;
holder.deleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GroceryList groceryList = GroceryList.get(getApplicationContext());
groceryList.deleteGrocery(grocery);
groceryAdapter.setGroceries(GroceryList.get(getApplicationContext()).getGroceries());
//holder.getAdapterPosition();
groceryAdapter.notifyItemRemoved(position);
groceryAdapter.notifyItemRangeRemoved(position, groceries.size());
Toast.makeText(getApplicationContext(),"Item deleted", Toast.LENGTH_SHORT).show();
//finish();
}
});
}
@Override
public int getItemCount(){
return groceries.size();
}
}
}
这是logcat
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{426a0900 position=0 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6624)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:812)
at android.view.Choreographer.doCallbacks(Choreographer.java:612)
at android.view.Choreographer.doFrame(Choreographer.java:582)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:798)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
您不应将final int position
传递给匿名new View.OnClickListener()
相反,请使用holder.getAdapterPosition()
:
private class GroceryAdapter extends RecyclerView.Adapter<GroceryHolder> {
private List<Grocery> groceries;
public GroceryAdapter(List<Grocery> groceries) {
this.groceries = groceries;
}
@Override
public GroceryHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutInflater= LayoutInflater.from(GroceryActivity.this);
View view=layoutInflater.inflate(R.layout.grocery_list_row, parent, false);
holder = new GroceryHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final GroceryHolder holder, int position){
int safePosition = holder.getAdapterPosition();
final Grocery grocery = groceries.get(safePosition);
holder.bindGrocery(grocery);
holder.deleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// delete item from the list
groceries.remove(safePosition);
groceryAdapter.notifyItemRemoved(safePosition);
Toast.makeText(getApplicationContext(),"Item deleted", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount(){
return groceries.size();
}
}
此外,还有一些不相关的问题:
最好将setOnClickListener
设置为不在onBindViewHolder
,而在onCreateViewHolder
在构造函数中设置杂货列表的代码看起来过于复杂。将此列表作为参数从您的Activity / Fragment传递可能要简单得多。
private GroceryHolder holder
因为实例变量是不必要的
以下是holder.getAdapterPosition()
https://youtu.be/imsr8NrIAMs?t=2092