简体   繁体   中英

Can we get two return types from getItemCount() in RecyclerView?

This is my code. when i say return data1(); it runs fine but when i try return data1() + data3(); it gives me indexOutofBound Excception and i don't know the reason. Can someone help me please to find the reason of this error??
This is my MainActivity class code

 String[] data={"ok","this"};
int []images_id = new int[2];
    for(int i=0; i<2; i++){
        images_id[i] = getResources().getIdentifier("drawable/"+"candy_"+i, null, getPackageName());
    }
    ArrayList ok=new ArrayList();
    for (int y=0;y<data.length;y++)
    {
         ok.add(data[y]);
    }
    recyclerView.setLayoutManager(new LinearLayoutManager(getParent()));
    RecyclerWorkclass recyclerWorkclass=new RecyclerWorkclass(this,data,images_id);
    recyclerView.setAdapter(recyclerWorkclass);

And now this iss my RecyclerView class code

   ArrayList data1,data3;
Context context;
LayoutInflater inflater;
public RecyclerWorkclass(Context context,String[] dataoftext,int[] data2)
{
    inflater= LayoutInflater.from(context);
    this.context=context;
    data1=new ArrayList();
    data3=new ArrayList();
    for (int y=0;y<data2.length;y++)
    {
        this.data3.add(data2[y]);
    }
    for (int i=0;i<dataoftext.length;i++)
    {
        data1.add(dataoftext[i]);
    }
    Toast.makeText(context,"size is"+data3.size(),Toast.LENGTH_LONG).show();
}
@Override
public MyAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.recyclerwork_txt,parent,false);
    MyAdapter myAdapter=new MyAdapter(view);
    Toast.makeText(context,"view is "+viewType,Toast.LENGTH_LONG).show();
    return myAdapter;
}
@Override
public void onBindViewHolder(MyAdapter holder, int position) {
    holder.imageView.setImageResource((Integer) data3.get(position));
    holder.textView.setText((String)data1.get(position));
}
@Override
public int getItemCount() {
    return data1.size();
}
public class MyAdapter extends RecyclerView.ViewHolder {
    TextView textView;
    ImageView imageView;
    public MyAdapter(View itemView) {
        super(itemView);
        textView=itemView.findViewById(R.id.textview_phonecompany);
        imageView=itemView.findViewById(R.id.imageView2);}}
}

this code works fine but when i say

return data1.size() + data3.size();

it shows index out of bound error. why is that?

Is because when you put data1.size() + data3.size() in getItemCount() you not relative with your onBindViewHolder() and this is can be call anytime RecyclerView.Adapter Doc .

When your adapter call the onBindViewHolder() he throw this exception because this must call getItemCount() method and your two list as not the size declared in getItemCount() .

Explaination

@Override
public void onBindViewHolder(MyAdapter holder, int position) {
   holder.imageView.setImageResource((Integer) data3.get(position));
   holder.textView.setText((String)data1.get(position));
}

In this the position will more than your list because you double the size. So when he try to do data3.get(position) this exception append.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM