简体   繁体   English

Android不知道如何解决此索引超出范围FirebaseRecyclerview

[英]Android Dont know how to fix this index out of bounds exception FirebaseRecyclerview

I get this error and I dont know how to fix it 我收到此错误,我不知道如何解决

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) java.lang.IndexOutOfBoundsException:Index:2,Size:2 at com.tijdelijk.java.util.ArrayList.get(ArrayList.java:411)。com.tijdelijk.firebasetest.Start $ 1.populateViewHolder(Start.java:69) .firebasetest.Start $ 1.populateViewHolder(Start.java:64)

I am working with firebase database and based on CategoryId I put the items in a arraylist for the SubCategories my code: 我正在使用Firebase数据库,并且基于CategoryId,我将这些项目放在SubCategories的数组列表中,我的代码是:

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) {

                    }
                });
    }
}

in this activity I want to display also a recyclerview but this time based on the arraylist I got from the categoryfragment here is my code: 在此活动中,我还想显示一个recyclerview,但是这次基于我从categoryfragment获得的arraylist,这是我的代码:

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);
                }
            });
        }


        };
    }
}

here is my viewholder: 这是我的观察者:

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 means that you are trying to access a value that is at an index out of the array. IndexOutOfBoundException意味着您正在尝试访问位于数组索引之外的值。

In your case, you have an array of two values. 在您的情况下,您有两个值的数组。 So you have the keys 0 and 1. As said in the error, your code is trying to access the index 2, which does not exist. 因此,您具有键0和1。如错误中所述,您的代码正在尝试访问不存在的索引2。

You have to check that you are accessing an index that is in the range of values of the array. 您必须检查是否正在访问数组值范围内的索引。

Good day to you. 祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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