[英]Launching BottomSheet from a RecyclerViewAdapter that extends with RecyclerView.Adapter inside onBindViewHolder
[英]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;
抛出IndexOutOfBoundsException。 我也尝试使用Loop,但没有用。
我的Pojo课堂。
public class AllClassDataResp {
@SerializedName("classNum")
@Expose
private Integer classNum;
@SerializedName("sectionInfos")
@Expose
private List<SectionInfo> sectionInfos = null;
谁能告诉我如何解决这个问题。
编辑:
使用for循环后
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);
它会引发IndexOutOfBoundsException
异常,因为您已经从列表中获取了对象。 您可以访问它的子对象。
AllClassDataResp allClassDataResp = allClassDataRespList.get(position);
SectionInfo sectionInfo = list.get(position); // throwing IndexOutOfBounds Exception
您的列表可能没有包含足够的数据。 您的列表大小小于您的排名,并且您尝试获取那些不存在的值。
您可以像这样获得列表值:
for(SectionInfo sectionInfo : list){
// your actual sectionInfo object get here.
}
这里的问题是您的变量列表没有与变量allClassDataRespList相同的项目数。 因此,您尝试访问不存在的索引处的变量。
您的代码allClassDataRespList.get(position)
正在访问超出可用索引的索引。 假设您有以下数组
allClassDataRespList = new ArrayList<AllClassDataResp>();
allClassDataRespList.add(new AllClassDataResp(...) ); //index 0
allClassDataRespList.add(new AllClassDataResp(...) ); //index 1
allClassDataRespList.add(new AllClassDataResp(...) ); //index 2
现在假设您具有从列表中访问对象的函数
public AllClassDataResp getItem(int position)
{
return allClassDataRespList.get(position);
}
现在说明您遇到的错误,让我们调用函数
AllClassDataResp existing1 = getItem(0); //works fine
AllClassDataResp existing2 = getItem(1); //works fine
AllClassDataResp nonexisting = getItem(3); //throws IndexOutOfBoundsException
最常识是检查数组大小是否确实以索引存在的方式
if(position < allClassDataRespList.size())
{
//exists
}
作为onBindViewHolder
方法,我还没有找到用例,其中没有绑定的项。 您的Adapater基本设计及其如何管理数据一定有问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.