我有这个错误 java.lang.IndexOutOfBoundsException:

我收到一个错误:


java.lang.IndexOutOfBoundsException:索引:0,大小:0


我遇到错误的部分在我的 if 语句中onBindViewHolder,下面是我的适配器:


public class UserProgramWeekAdapter extends RecyclerView.Adapter<UserProgramWeekAdapter.ViewHolder>{


    private Context context;

    private ArrayList<ProgramTrackerWrapper> programTrackers;




    public UserProgramWeekAdapter(Context context, ArrayList<ProgramTrackerWrapper> programTrackers) {

        this.context = context;

        this.programTrackers = programTrackers;

    }


    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_user_program_week_adapter, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;


    }


    @Override

    public void onBindViewHolder(UserProgramWeekAdapter.ViewHolder holder, int position) {

        holder.dayTxt.setText(programTrackers.get(position).getDay());

        holder.numberofExercises.setText(programTrackers.get(position).getProgramTrackers().size()+ " exercises");

        boolean isFinished = false;

        for (int i = 0; i < programTrackers.size(); i++){

        if ( programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()){

                isFinished = true;

            }

        }

        String statusTxt = isFinished || programTrackers.size() == 0 ? "Completed" : "Not Completed";

        holder.exerciseCompleted.setText(statusTxt);

    }


    @Override

    public int getItemCount() {

        return programTrackers.size();

    }


    public class ViewHolder extends RecyclerView.ViewHolder {


        TextView dayTxt, exerciseCompleted, numberofExercises;


        public ViewHolder(View itemView) {


            super(itemView);


            dayTxt = itemView.findViewById(R.id.dayTxt);

            exerciseCompleted = itemView.findViewById(R.id.exerciseCompleted);

            numberofExercises = itemView.findViewById(R.id.numberofExercises);

        }

    }

}


米脂
浏览 101回答 1
1回答

暮色呼如

这看起来很可疑:for (int i = 0; i < programTrackers.size(); i++){&nbsp; if ( programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()){&nbsp; &nbsp; &nbsp; &nbsp; isFinished = true;&nbsp; &nbsp; }}您检查programTrackers列表的大小,但不检查其子列表,因此i很容易指向不正确的索引:final int sublistSize =&nbsp; programTrackers.get(position).getProgramTrackers().size();for (int i = 0; i < sublistSize; i++){&nbsp; if (programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()){&nbsp; &nbsp; &nbsp; &nbsp; isFinished = true;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java