web-dev-qa-db-ja.com

RecyclerViewおよびJava.lang.IndexOutOfBoundsException無効なビューホルダーアダプターpositionViewHolder

食料品をバインドする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)
7
user7970860

_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 の詳細はこちらです

4
dkarmazi