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