Kim E. R
Kim E. R

Reputation: 73

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 in recyclerview

I need to retrieve a list of name from Firebase, after that the name list can display by using recyclerview. However, it displays java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 in my logcat which showed the error has happened in recyclerview class.Thanks in advance whoever has helped me :)

Below is the line which has the error :

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

I don't know what's wrong to my code. Here is my recyclerview class

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

Here is how I retrieve my data

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

here is my firebase structure table

Upvotes: 1

Views: 2753

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599621

It looks like you have fewer items in namelist than there are in alist.

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

Since you tell the recycler view there are alist.size() items in the view, that's how many items it tries to draw. But then at some point, there is no corresponding item in namelist, so it throws an error.

The solution is likely to either make sure you have the same number of items in alist and namelist, or to only render the number of items you have in namelist.

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

Upvotes: 4

Related Questions