RecyclerView 嵌套 java.lang.IndexOutOfBoundsException: 索引 5 无效,大小为 5

RecyclerView Nesting java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5

场景:我在 recyclerview 中有 5 个数据,并尝试添加 addOnItemTouchListener。第一个 运行 在 1 到 3 RecyclerView 行中表现良好。但是如果我点击第 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

这是我的适配器

public class PengumumanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    List<ListItem> consolidatedList = new ArrayList<>();

    public PengumumanAdapter(Context context, List<ListItem> consolidatedList) {
        this.consolidatedList = consolidatedList;
        this.mContext = context;

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {

            case ListItem.TYPE_TASK:
                View v1 = inflater.inflate(R.layout.costume_row_pengumuman, parent,
                        false);
                viewHolder = new GeneralViewHolder(v1);
                break;

            case ListItem.TYPE_NAME:
                View v2 = inflater.inflate(R.layout.costum_row_pengumuman_name, parent, false);
                viewHolder = new DateViewHolder(v2);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        switch (viewHolder.getItemViewType()) {

            case ListItem.TYPE_TASK:

                GeneralItem generalItem   = (GeneralItem) consolidatedList.get(position);
                GeneralViewHolder generalViewHolder= (GeneralViewHolder) viewHolder;
                generalViewHolder.txtJudul.setText(generalItem.getDaftarPengumuman().getJudul());
                generalViewHolder.txtDeskripsi.setText(generalItem.getDaftarPengumuman().getDeskripsi());
                generalViewHolder.txtTanggal.setText(generalItem.getDaftarPengumuman().getTanggal_peng());


                break;

            case ListItem.TYPE_NAME:
                PengumumanItem dateItem = (PengumumanItem) consolidatedList.get(position);
                DateViewHolder dateViewHolder = (DateViewHolder) viewHolder;

                dateViewHolder.txtName.setText(dateItem.getNama_matkul());
                // Populate date item data here

                break;
        }
    }

    // ViewHolder for date row item
    class DateViewHolder extends RecyclerView.ViewHolder {
        protected TextView txtName;

        public DateViewHolder(View v) {
            super(v);
            this.txtName = (TextView) v.findViewById(R.id.name_p);

        }
    }

    // View holder for general row item
    class GeneralViewHolder extends RecyclerView.ViewHolder {
        protected TextView txtJudul,txtDeskripsi,txtTanggal;

        public GeneralViewHolder(View v) {
            super(v);
            this.txtJudul = (TextView) v.findViewById(R.id.judul_p);
            this.txtDeskripsi = (TextView) v.findViewById(R.id.deskripsi_p);
            this.txtTanggal = (TextView) v.findViewById(R.id.tanggal_p);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return consolidatedList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return consolidatedList != null ? consolidatedList.size() : 0;
    }
}

解决方案

在解决之前,这个崩溃的问题是hashmap列表和recyclerview临时大小列表class不同。我正在使用嵌套数组列表 .

我从@Suraj Vaishnav 找到了这个解决方案。 1.从consolidatedList获取数据,因为这个数组传给了adapter。 2. 为 consolidatelist >= position 创建条件 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) {

        }
    }));

position 参数是在recycler view 中点击的item 的索引,这里不应该使用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));

您对不同的数组使用相同的参数(位置)。确保每个都具有相同的大小。

--新---

问题是consolidatedList size和listPengumuman不一样。

要避免此崩溃,您可以执行以下操作:

if(listPengumuman.size()-1 >= position)
{
 DaftarPengumuman pengumuman = listPengumuman.get(position);
}

尝试使用此代码获取数据。

Object  data = consolidatedList.get(position);
        if (data instanceof  GeneralItem){
            DaftarPengumuman pengumuman = data.getDaftarPengumuman();
        }