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

时间:2018-09-23 14:29:38

标签: java android arrays android-recyclerview indexoutofboundsexception

场景: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

这是我的适配器

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 temp大小的列表类不同。我正在使用嵌套数组列表 here

我从@Suraj Vaishnav找到了这种甜味精。 1.因为此数组传递到适配器,所以从solidifiedList获取数据。 2.使合并条件> =位置 3.放入Extra并创建GeneralItem Object,然后从中获取数据。

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

        }
    }));

2 个答案:

答案 0 :(得分:0)

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

您将相同的参数(位置)用于不同的数组。确保它们每个都有相同的大小。

-新---

问题在于consolidatedList的大小和listPengumuman不相同。

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

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

答案 1 :(得分:0)

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

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