简体   繁体   中英

how to extract a list from another list in OnBindViewHolder in RecyclerView.Adapter

@Override
public void onBindViewHolder(@NonNull MyViewHolder45 holder, int position)
{
    AllClassDataResp allClassDataResp = allClassDataRespList.get(position);
    int classNumb = allClassDataResp.getClassNum();
    List<SectionInfo> list = allClassDataResp.getSectionInfos();
    SectionInfo sectionInfo = list.get(position); // throwing IndexOutOfBounds Exception

    String name = sectionInfo.getSectionName();
    Long id = sectionInfo.getSectionId();
    String classandSec = classNumb + "th" + " - " + name;

    holder.tClassSec.setText(classandSec);
    holder.sectionInfo = sectionInfo;

Throwing IndexOutOfBoundsException. I also tried using for Loop but didn't work.

My Pojo Class.

public class AllClassDataResp {


@SerializedName("classNum")
@Expose
private Integer classNum;

@SerializedName("sectionInfos")
@Expose
private List<SectionInfo> sectionInfos = null;

Can Anyone Tell me how to solve this.

EDITED :

after using for Loop

AllClassDataResp allClassDataResp = allClassDataRespList.get(position);
    int classNumb = allClassDataResp.getClassNum();

    List<SectionInfo> sectionInfoList = allClassDataResp.getSectionInfos();

    String classAndSec = "";

    for (SectionInfo sectionInfo : sectionInfoList)
    {
        String name = sectionInfo.getSectionName();

        classAndSec = classNumb + "th" + " - " + name;
    }

    holder.tClassSec.setText(classAndSec);

It throw IndexOutOfBoundsException exception because you are already get your object from your list. you have access it sub object.

AllClassDataResp allClassDataResp = allClassDataRespList.get(position);

SectionInfo sectionInfo = list.get(position); // throwing IndexOutOfBounds Exception

May be possible your list do not contain enough data. Your list size are less than your position and you are try to get those value which is not exist.

You can get the list value like :

for(SectionInfo sectionInfo : list){
   // your actual sectionInfo object get here.
}

The issue here is that your variable list doesn't have the same number of item than the variable allClassDataRespList. So you are trying to access a variable at a non existing index.

Your code allClassDataRespList.get(position) is accessing index that is beyond what is available. Suppose you have the following array

allClassDataRespList = new ArrayList<AllClassDataResp>();
allClassDataRespList.add(new AllClassDataResp(...) ); //index 0
allClassDataRespList.add(new AllClassDataResp(...) ); //index 1
allClassDataRespList.add(new AllClassDataResp(...) ); //index 2

Now assume you have your function that accesses the objects from the list

public AllClassDataResp getItem(int position)
{
    return allClassDataRespList.get(position);
}

now to illustrate error you are getting, let us call our function

AllClassDataResp existing1 = getItem(0); //works fine
AllClassDataResp existing2 = getItem(1); //works fine
AllClassDataResp nonexisting = getItem(3); //throws IndexOutOfBoundsException

The most common sense would be checking if array size is indeed in such a way that index exists

if(position < allClassDataRespList.size())
{
    //exists
}

See more on that method on the Documentation

As onBindViewHolder method of the RecyclerView Adapater, I have never found yet a use case where am given an item to bind that does not exist. There must be a problem with your basic design of the Adapater and how it manages data

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