为了账号安全,请及时绑定邮箱和手机立即绑定

RecyclerView 嵌套 java.lang.IndexOutOfBounds

RecyclerView 嵌套 java.lang.IndexOutOfBounds

暮色呼如 2021-10-06 13:01:18
场景:我在 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
查看完整描述

3 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

解决方案


在解决之前,这个崩溃的问题是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) {


        }

    }));


查看完整回答
反对 回复 2021-10-06
?
绝地无双

TA贡献1946条经验 获得超4个赞

试试这个代码来获取数据。


Object  data = consolidatedList.get(position);

        if (data instanceof  GeneralItem){

            DaftarPengumuman pengumuman = data.getDaftarPengumuman();

        }


查看完整回答
反对 回复 2021-10-06
?
神不在的星期二

TA贡献1963条经验 获得超6个赞

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);

}


查看完整回答
反对 回复 2021-10-06
  • 3 回答
  • 0 关注
  • 169 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信