猿问

RecyclerView 嵌套 java.lang.IndexOutOfBounds

场景:我在 recyclerview 中有 5 个数据,并尝试添加addOnItemTouchListener. 首先在 1 到 3 RecyclerView Row 中运行良好。但是,如果我单击 4 和 5 行,则会出现该异常。


代码:


 mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new RecyclerTouchListener.ClickListener() {

            @Override

            public void onClick(View view, int position) {


                DaftarPengumuman pengumuman = listPengumuman.get(position-1);


                Intent intent = new Intent(getActivity(), DetailPengumuman.class);

//                intent.putExtra("keys", keys.get(position));

                intent.putExtra("namamatkul", namaMatkulPut.get(position-1));

                intent.putExtra("namapengumuman", namaPengumumanPut.get(position-1));

                intent.putExtra("tanggalpengumuman", tanggalPengumumanPut.get(position-1));

                intent.putExtra("judulpengumuman", judulPengumumanPut.get(position-1));

                intent.putExtra("deskripsipengumuman", deskripsiPengumumanPut.get(position-1));

                startActivity(intent);


            }


            @Override

            public void onLongClick(View view, int position) {


            }

    }));

在我在位置上做 -1 之前,异常说:


java.lang.IndexOutOfBoundsException: Invalid index 6, size is 5


暮色呼如
浏览 170回答 3
3回答

慕尼黑5688855

解决方案在解决之前,这个崩溃的问题是recyclerview temp size的hashmap列表和列表类不同。我在这里使用嵌套数组列表 。我从@Suraj Vaishnav 找到了这个解决方案。1. 获取来自solidatedList 的数据,因为这个数组被传递给了适配器。2. 为consolidatelist >= position 创建Condition 3. 放置Extra 并创建GeneralItem 对象,并从中获取数据。    mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getContext(), mRecyclerView, new RecyclerTouchListener.ClickListener() {        @Override        public void onClick(View view, int position) {            if(consolidatedList.size()-1 >= position)            {                GeneralItem generalItem   = (GeneralItem) consolidatedList.get(position);                Intent intent = new Intent(getActivity(), DetailPengumuman.class);                intent.putExtra("namamatkul", generalItem.getDaftarPengumuman().getNama_p());                intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng());                intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul());                intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi());                startActivity(intent);            }        }        @Override        public void onLongClick(View view, int position) {        }    }));

绝地无双

试试这个代码来获取数据。Object  data = consolidatedList.get(position);        if (data instanceof  GeneralItem){            DaftarPengumuman pengumuman = data.getDaftarPengumuman();        }

神不在的星期二

position 参数是在回收器视图中单击的项目的索引,此处不应使用 position-1。只需使用位置。另一件事是DaftarPengumuman pengumuman = listPengumuman.get(position-1);这可能工作正常。但在这些陈述中:intent.putExtra("namamatkul", namaMatkulPut.get(position-1));                intent.putExtra("namapengumuman", namaPengumumanPut.get(position-1));                intent.putExtra("tanggalpengumuman", tanggalPengumumanPut.get(position-1));                intent.putExtra("judulpengumuman", judulPengumumanPut.get(position-1));                intent.putExtra("deskripsipengumuman", deskripsiPengumumanPut.get(position-1));您对不同的数组使用相同的参数(位置)。确保它们每个都具有相同的尺寸。- 新的 - -问题是综合列表大小和列表企鹅不一样。为了避免这种崩溃,您可以执行以下操作:if(listPengumuman.size()-1 >= position){ DaftarPengumuman pengumuman = listPengumuman.get(position);}
随时随地看视频慕课网APP

相关分类

Java
我要回答