gpt4 book ai didi

java - Android 不知道如何修复此索引越界异常 FirebaseRecyclerview

转载 作者:行者123 更新时间:2023-12-02 01:52:05 24 4
gpt4 key购买 nike

我收到此错误,但我不知道如何修复它

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.ArrayList.get(ArrayList.java:411) at com.tijdelijk.firebasetest.Start$1.populateViewHolder(Start.java:69) at com.tijdelijk.firebasetest.Start$1.populateViewHolder(Start.java:64)

我正在使用 firebase 数据库,并根据 CategoryId 将项目放入子类别的数组列表中,我的代码:

public class CategoryFragment extends Fragment {

View myFragment;

RecyclerView listCategory;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;

FirebaseDatabase database;

DatabaseReference categories;
DatabaseReference subCategory;

public static CategoryFragment newInstance() {
CategoryFragment fragment = new CategoryFragment();
return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

database = FirebaseDatabase.getInstance();
categories = database.getReference("Category");
subCategory = database.getReference("SubCategory");

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myFragment = inflater.inflate(R.layout.fragment_category, container, false);

listCategory = (RecyclerView) myFragment.findViewById(R.id.listCategory);
listCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(container.getContext());
listCategory.setLayoutManager(layoutManager);

loadCategories();


return myFragment;
}

private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories
) {


@Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
viewHolder.category_name.setText(model.getName());
Picasso.with(getActivity())
.load(model.getImage())
.into(viewHolder.category_image);

viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent startGame = new Intent(getActivity(), Start.class);
Common.categoryId = adapter.getRef(position).getKey();
loadSubCategory(Common.categoryId);

startActivity(startGame);
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}

private void loadSubCategory(String categoryId) {

//Clear list if there are old subCategory
if (Common.subCategoryList.size() > 0) {
Common.subCategoryList.clear();
}

subCategory.orderByChild("CategoryId").equalTo(categoryId)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
SubCategory ques = postSnapshot.getValue(SubCategory.class);
Common.subCategoryList.add(ques);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
}

在此 Activity 中,我还想显示一个回收器 View ,但这一次基于我从类别 fragment 获得的数组列表,这是我的代码:

public class Start extends AppCompatActivity {


FirebaseDatabase database;
DatabaseReference subCategory;

FirebaseRecyclerAdapter<SubCategory, SubCategoryViewHolder> adapter;
RecyclerView listSubCategory;
RecyclerView.LayoutManager layoutManager;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);

database = FirebaseDatabase.getInstance();
subCategory = database.getReference("SubCategory");

listSubCategory = findViewById(R.id.listSubCategory);
listSubCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getBaseContext());
listSubCategory.setLayoutManager(layoutManager);

loadSubCategories();
adapter.notifyDataSetChanged();
listSubCategory.setAdapter(adapter);
}




private void loadSubCategories() {
adapter = new FirebaseRecyclerAdapter<SubCategory, SubCategoryViewHolder>(
SubCategory.class,
R.layout.subcategory_layout,
SubCategoryViewHolder.class,
subCategory
) {


@Override
protected void populateViewHolder(SubCategoryViewHolder viewHolder, SubCategory model, int position) {
viewHolder.subcategory_nlname.setText(Common.subCategoryList.get(position).getLatijnseNaam());
viewHolder.subcategory_ltname.setText(Common.subCategoryList.get(position).getNederlandseNaam());


viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent startGame = new Intent(Start.this, Start.class);
Common.categoryId = adapter.getRef(position).getKey();

startActivity(startGame);
}
});
}


};
}
}

这是我的观看者:

public class SubCategoryViewHolder extends RecyclerView.ViewHolder 
implements View.OnClickListener{

public TextView subcategory_nlname;
public TextView subcategory_ltname;


private ItemClickListener itemClickListener;



public SubCategoryViewHolder(View itemView) {
super(itemView);
subcategory_ltname = itemView.findViewById(R.id.latijnse_naam);
subcategory_nlname = itemView.findViewById(R.id.nederlandse_naam);

// itemView.findViewById(R.id.btnPlay).setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}

最佳答案

IndexOutOfBoundException 表示您正在尝试访问位于数组外索引处的值。

就您而言,您有一个包含两个值的数组。所以你有键 0 和 1。正如错误中所述,你的代码正在尝试访问不存在的索引 2。

您必须检查您正在访问的索引是否在数组值的范围内。

祝你有美好的一天。

关于java - Android 不知道如何修复此索引越界异常 FirebaseRecyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52813885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com