3

我有一個回收站視圖,該視圖綁定一個雜貨商品。添加該項目的作品完美。但是,當我嘗試刪除該應用程序崩潰的項目時,我得到IndexOutOfBoundsException錯誤。RecyclerView和java.lang.IndexOutOfBoundsException無效的視圖持有者適配器positionViewHolder

我面臨的問題是在我的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) 

回答

3

你不應該傳遞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(); 
     } 
    } 

此外,還有幾個不相關的問題:

  1. 倒不如設置setOnClickListeneronBindViewHolder,但在onCreateViewHolder

  2. 代碼周圍設置雜貨清單在構造函數中看起來過於複雜。將這個列表作爲活動/片段的參數傳遞可能會簡單得多。

  3. private GroceryHolder holder作爲實例變量是不必要

這裏是關於holder.getAdapterPosition()https://youtu.be/imsr8NrIAMs?t=2092

多個上下文
相關問題