【发布时间】: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