【问题标题】:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 in recyclerviewjava.lang.IndexOutOfBoundsException: Index: 0, Size: 0 in recyclerview
【发布时间】:2019-12-10 19:04:55
【问题描述】:

我需要从 Firebase 中检索名称列表,然后可以使用 recyclerview 显示名称列表。但是,它在我的 logcat 中显示 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 显示错误发生在 recyclerview 类中。提前感谢帮助我的人:)

下面是有错误的行:

holder.tname.setText(namelist1.get(position));

我不知道我的代码有什么问题。 这是我的recyclerview类

public class MyRecyclerviewPAppointment extends RecyclerView.Adapter<MyRecyclerviewPAppointment.MyViewHolder> {
    private Context context;
    ArrayList<AppointmentObject> alist;
ArrayList<String> namelist1;

    public MyRecyclerviewPAppointment(Context c, ArrayList<AppointmentObject> t,ArrayList<String> namelist) {
        context = c;
        alist = t;
        namelist1=namelist;
    }
 @Override
    public void onBindViewHolder(@NonNull final MyRecyclerviewPAppointment.MyViewHolder holder,final int position) {

     holder.tdate.setText(alist.get(position).getDate());
      holder.ttime.setText(alist.get(position).getTiming());
////////////error happened here////////////////////////
        holder.tname.setText(namelist1.get(position));

    }

    @Override
    public int getItemCount() {
        return alist.size();
    }
}

这是我检索数据的方式

 databaseReference= FirebaseDatabase.getInstance().getReference().child("appointment");
        databaseReference.orderByChild("userid").equalTo(userid1).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {


                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {

                    AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
                    a.add(thera);
                    final String theraid = dataSnapshot1.child("theraid").getValue(String.class);


                    DatabaseReference refThera = FirebaseDatabase.getInstance().getReference().child("alluser").child("thera");
                    refThera.child(theraid).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot3) {

                            for (DataSnapshot ds: dataSnapshot3.getChildren())
                            {
                                String text = ds.child("name").getValue(String.class);
                                namelist.add(text);
                            }
                        }


                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
                            throw databaseError.toException();
                        }
                    });

                }

                adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a,namelist);
                rv.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Oh no!", Toast.LENGTH_SHORT).show();
            }
        });

这是我的火力基地结构

【问题讨论】:

  • 请仅使用 android-studio 标签来回答有关 Android Studio IDE 本身的问题。对于一般的 Android 编程问题,请使用android 标签。

标签: android arraylist firebase-realtime-database android-recyclerview


【解决方案1】:

您在namelist 中的项目似乎比alist 中的项目少。

public int getItemCount() {
    return alist.size();
}       

由于您告诉回收站视图在视图中有alist.size() 项目,这就是它尝试绘制的项目数量。但是在某个时候,namelist 中没有对应的项目,所以它会抛出一个错误。

解决方案可能是确保alistnamelist 中的项目数量相同,或者只呈现namelist 中的项目数量。

public int getItemCount() {
    return namelist.size();
}       

【讨论】:

  • 我已经编辑了我的问题,实际上我认为 ArrayList 有相同数量的元素?在第一个循环中,它获取与当前登录的用户对应的项目。在第二个循环中,它根据从第一个循环中检索到的一个特定数据获取项目
  • "我认为 ArrayList 的元素数量相同" 错误消息清楚地表明这不是真的。由于您没有展示如何填充alist,因此无法说明为什么它们的长度不同。如果您很难解决问题,我建议您将其隔离。这通常涉及在新应用程序中从头开始,然后添加最少的代码,直到遇到相同的问题。见how to create a minimal, complete, verifiable example
猜你喜欢
  • 2018-08-01
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2022-06-13
相关资源
最近更新 更多