В моем приложении я хочу отобразить 2 разных виджета с 2 списками данных массива в адаптере. Но при запуске я получаю эту ошибку.
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 в java.util.ArrayList.get (ArrayList.java:411) в gjteamcom.doubleteam.CariAdapter.onBindViewHolder (CariAdapter.java:58) в android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder (Recycler): 6781) на android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder (RecyclerView.java:6823) на android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline (RecyclerView.java:5752pp) и.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6019) в android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition (RecyclerView.java:5858) в android.support.Re7.ler. ReViewler.Re7.ler.getViewForPosition (RecyclerView.java:5854) в android.support.v7.widget.LinearLayoutManager $ LayoutState.next (LinearLayoutManager.java:2230)на android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1557) на android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1517) на android.support.ager.LayoutLaronLL(LinearLayoutManager.java:612) по адресу android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3924) по адресу android.support.v7.widget.RecyclerView.dispatchLayout (RecyclerView.jroid:support).v7.widget.RecyclerView.onLayout (RecyclerView.java:4194) на android.view.View.layout (View.java:17838) на android.view.ViewGroup.layout (ViewGroup.java:5754) на android.widget.LinearLayout.setChildFrame (LinearLayout.java:1982) в android.widget.LinearLayout.layoutVertical (LinearLayout.java:1826) в android.widget.LinearLayout.onLayout (LinearLayout.java:1735) в android.view.View.layout (представление.Java: 17838) на android.view.ViewGroup.layout (ViewGroup.java:5754) на android.support.constraint.ConstraintLayout.onLayout (ConstraintLayout.java:1915)на android.view.View.layout (View.java:17838) на android.view.ViewGroup.layout (ViewGroup.java:5754) на android.widget.FrameLayout.layoutChildren (FrameLayout.java:383) на android.widget.FrameLayout.onLayout (FrameLayout.java:321) в android.view.View.layout (View.java:17838) в android.view.ViewGroup.layout (ViewGroup.java:5754) в android.widget.LinearLayout.setChildFrame (LinearLayout.java: 1982) на android.widget.LinearLayout.layoutVertical (LinearLayout.java:1826) на android.widget.LinearLayout.onLayout (LinearLayout.java:1735) на android.view.View.layout (View.java:17838)
Вот мой адаптер:
public class CariAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
ArrayList<Cari> cari;
ArrayList<CariP> carip;
public CariAdapter(Context c, ArrayList<Cari> p, ArrayList<CariP> pp)
{
context = c;
cari = p ;
carip = pp;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
switch (i) {
case 0:
return new CariViewHolder(LayoutInflater.from(context).inflate(R.layout.cv_cari,viewGroup,false));
case 2:
return new CariPViewHolder(LayoutInflater.from(context).inflate(R.layout.cv_carip,viewGroup,false));
default:
return null;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
switch (viewHolder.getItemViewType()) {
case 0:
CariViewHolder cariViewHolder = (CariViewHolder) viewHolder;
cariViewHolder.timnama.setText(cari.get(i).getTim_nama());
cariViewHolder.timkat.setText(cari.get(i).getTim_kategori());
Picasso.get().load(cari.get(i).getTim_image()).resize(200,200).into(cariViewHolder.timimg);
break;
case 2:
CariPViewHolder cariPViewHolder = (CariPViewHolder) viewHolder;
cariPViewHolder.akunanama.setText(carip.get(i).getAkun_nickname());
cariPViewHolder.akuninterest.setText(carip.get(i).getAkun_interest());
Picasso.get().load(carip.get(i).getAkun_profilimage()).resize(200,200).into(cariPViewHolder.akunimg);
break;
default:
}
}
@Override
public int getItemViewType(int position) {
return position % 2 * 2;
}
@Override
public int getItemCount() {
return cari.size()+carip.size();
}
class CariViewHolder extends RecyclerView.ViewHolder
{
TextView timnama,timkat;
ImageView timimg;
public CariViewHolder(@NonNull View itemView) {
super(itemView);
timnama = itemView.findViewById(R.id.txt_cvnamacari);
timkat = itemView.findViewById(R.id.txt_cvkategori);
timimg = itemView.findViewById(R.id.img_cvcari);
}
}
class CariPViewHolder extends RecyclerView.ViewHolder
{
TextView akunanama,akuninterest;
ImageView akunimg;
public CariPViewHolder(@NonNull View itemView) {
super(itemView);
akunanama = itemView.findViewById(R.id.txt_cvpnamacari);
akuninterest = itemView.findViewById(R.id.txt_cvpinterest);
akunimg = itemView.findViewById(R.id.img_cvpcari);
}
}}
Я все еще не понимаю, как getItemCount()
и getItemViewType()
будут работать на BindViewHolder
, как я могу исправить эту ошибку?