RecyclerView-java.lang.IndexOutOfBoundsException:无效的索引1,大小为0

时间:2019-01-01 14:13:13

标签: java android android-recyclerview indexoutofboundsexception recycler-adapter

在我的应用中,我想在适配器中显示2个不同的Viewholder和2个数组列表数据。但是在运行时,出现此错误。

  

java.lang.IndexOutOfBoundsException:索引:1,大小:0           在java.util.ArrayList.get(ArrayList.java:411)           在gjteamcom.doubleteam.CariAdapter.onBindViewHolder(CariAdapter.java:58)           在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6781)           在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6823)           在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)           在android.support.v7.widget.RecyclerView $ Recycler.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.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)           在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)           在android.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(View.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上工作 我该如何解决该错误?

3 个答案:

答案 0 :(得分:0)

假设两个数组的大小相同。那么您需要找到合适的位置以从阵列中获取数据。我在这里代表您的回收站视图项目的位置,但不代表阵列项目的位置 这是onBindView函数

@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/2).getTim_nama());
            cariViewHolder.timkat.setText(cari.get(i/2).getTim_kategori());
            Picasso.get().load(cari.get(i/2).getTim_image()).resize(200,200).into(cariViewHolder.timimg);
            break;

        case 2:
            CariPViewHolder cariPViewHolder = (CariPViewHolder) viewHolder;
            cariPViewHolder.akunanama.setText(carip.get(i/2).getAkun_nickname());
            cariPViewHolder.akuninterest.setText(carip.get(i/2).getAkun_interest());
            Picasso.get().load(carip.get(i/2).getAkun_profilimage()).resize(200,200).into(cariPViewHolder.akunimg);
            break;

            default:

    }
}

答案 1 :(得分:0)

您的代码在两个地方引起问题。

  1. 有一个隐式假设cari.size() == carip.size()cari.size() = carip.size() + 1。您可能已经知道这一点。只需添加此内容以确保您了解它即可。

  2. onBindViewHolder中,您不能直接使用i(头寸)。考虑以下情况:cari的内容为C 0 ,C 1 ,而carip的内容为P 0

与每个项目相关联的位置(i s)将为:C 0 :0,P 0 :1和C 1 < / sub>:2。

您现在可以看到问题。这里,carip.size() == 1表示最大索引为0。但是i中使用的位置(onBindViewHolder)将为1。这超出了范围。

您需要在i中将i/2的用法替换为onBindViewHolder

这将导致新的关联:C 0 :0,P 0 :0和C 1 :1正是这样的关联你想要

答案 2 :(得分:0)

您对使用不同的viewType有一些误解。你的问题是这个功能

@Override
public int getItemCount() {
    return cari.size()+carip.size();
}

您必须具有包含viewType和Model的单个列表项。建议您阅读此article,并了解如何创建itemModel

这很难解释您的代码,但不要害羞地问我会回答并帮助您的问题