0

Scenario : I have 5 data in recyclerview, and tried to adding addOnItemTouchListener. First run doing well in 1 until 3 RecyclerView Row. But if I click on 4 and 5 row , it got that exception.

Code:

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

            }
    }));

Before I did -1 in position, the exception says:

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

This is my adapter

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

SOLUTION

Before the solution, the problem of this crash is list of hashmap and list class for recyclerview temp size is different. Im using nesting array list here.

I found this solutin from @Suraj Vaishnav. 1. Get data from consolidatedList because this array is passed to adapter. 2. Make Condition for consolidatelist >= position 3. Put Extra and make GeneralItem Object, and get the data from it.

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

        }
    }));
  • It seems that there is some issue with listPengumuman list. Please check the data and count of that list. – Jay Thakkar Sep 23 '18 at 14:35
  • The data is correct, it shows me 5 data – yehezkiel yehezkiel Sep 23 '18 at 14:37
  • You issue is in DaftarPengumuman pengumuman = listPengumuman.get(position-1) some how you retrieving data for invalid position. But as you say your data is correct then share your code of setting adapter. – Jay Thakkar Sep 23 '18 at 14:40
  • That is my adapter sir @Jay – yehezkiel yehezkiel Sep 23 '18 at 14:41
  • Please check hashMap() method. Your size of consolidatedList and listPengumuman will be different. – Jay Thakkar Sep 23 '18 at 14:48
  • The consolidatedList shows me 9 and listpengumuman 5, how to fix this? – yehezkiel yehezkiel Sep 23 '18 at 14:53
  • Try to get data consolidatedList list instead of listpengumuman . – Jay Thakkar Sep 23 '18 at 14:58
  • Please provice with code @Jay – yehezkiel yehezkiel Sep 23 '18 at 14:58
  • Is you GeneralItem value is 5? – Jay Thakkar Sep 23 '18 at 15:06
  • And two hints: when asking questions here please provide a [mcve]. That includes to spent the time required to properly format and indent it. And for your own sake and sanity: study clean code, and learn why it is super bad practice to push so many levels of nesting into a single method. I hardly could read your code, and it pretty natural that you, the author of this confusing spaghetti code now has to turn to other people to figure what said code is actually doing. – GhostCat Sep 23 '18 at 15:24
  • The main problem in this case are in the very top code and the array code sir, you dont have to read the full code actually. The code is really complex because of the some conditions needed. Then I don't think this is duplicate. Thats why I asking this question. Last, I also give the solution of my problem. @GhostCat – yehezkiel yehezkiel Sep 23 '18 at 16:27

2 Answers2

0

position parameter is index of item clicked in recycler view, you should not use position-1 here. Just use position. Another thing is DaftarPengumuman pengumuman = listPengumuman.get(position-1); this may work fine. But in these statements :

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

You are using same parameter(position) for different array. Make sure each of them have same size.

--new---

The problem is consolidatedList size and listPengumuman is not same.

To refrain from this crash you can do:

if(listPengumuman.size()-1 >= position)
{
 DaftarPengumuman pengumuman = listPengumuman.get(position);
}
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

Try this code to get data.

Object  data = consolidatedList.get(position);
        if (data instanceof  GeneralItem){
            DaftarPengumuman pengumuman = data.getDaftarPengumuman();
        }
Jay Thakkar
  • 1,392
  • 10
  • 19
  • This is quite correct in logic, but first time I seen this code. I've got confused. Explanation : You have to get consolidatedList instead of listPengumuman. Then when you putExtra you should send the consolidatedList data too. – yehezkiel yehezkiel Sep 23 '18 at 16:20