java.lang.IndexOutOfBoundsException:recyclerview中的索引:0,大小:0

时间:2019-08-02 07:18:40

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

我需要从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();
            }
        });

这是我的Firebase结构 table

1 个答案:

答案 0 :(得分:3)

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

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

由于您告诉回收者视图,该视图中有alist.size()个项目,因此它尝试绘制多少个项目。但是在某个时候,namelist中没有相应的项目,因此会引发错误。

该解决方案可能会确保您在alistnamelist中具有相同数量的项目,或者仅呈现namelist中具有相同数量的项目。

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