我遇到错误:
java.lang.IndexOutOfBoundsException:索引:0,大小:0
我遇到错误的部分是我的onBindViewHolder
上的if语句,下面是我的适配器:
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);
}
}
}
这是我的活动:
public class StartProgramActivity extends AppCompatActivity {
private Toolbar toolBar;
private TextView weekTitle, weekStateTxt;
private ImageView nextBtn, backBtn;
private Program program;
private DatabaseReference programTrackerReference;
private ArrayList<ProgramTracker> allProgramTrackers = new ArrayList<>();
private ArrayList<ProgramTracker> dayOfWeekExercises = new ArrayList<>();
private String TAG = "StartProgramActivity";
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<ProgramTrackerWrapper> programTrackerWrappers = new ArrayList<>();
private UserProgramWeekAdapter userProgramWeekAdapter;
private int totalWeeks;
private int currentWeek = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_program);
programTrackerReference = FirebaseDatabase.getInstance().getReference("ProgramTracker");
getDataFromPreviousActivity();
getProgramDetails(currentWeek);
findViews();
}
public void getDataFromPreviousActivity(){
if(getIntent().getSerializableExtra("program") != null) {
program = (Program) getIntent().getSerializableExtra("program");
totalWeeks = Integer.parseInt(program.getProgramWeeks());
}
}
private void setUpWeekNavigation(){
if(currentWeek == 1){
backBtn.setVisibility(View.GONE);
}else{
backBtn.setVisibility(View.VISIBLE);
}
if(totalWeeks > 1){
nextBtn.setVisibility(View.VISIBLE);
}
if(currentWeek == totalWeeks){
nextBtn.setVisibility(View.GONE);
}
}
private void determineWeek(int command){
// if 0 is passed, meaning back is pressed
// if 1 is passed, meaning next is pressed
switch (command){
case 0:
currentWeek--;
weekTitle.setText("Week " + currentWeek);
setUpWeekNavigation();
getProgramDetails(currentWeek);
break;
case 1:
currentWeek++;
weekTitle.setText("Week " + currentWeek);
setUpWeekNavigation();
getProgramDetails(currentWeek);
break;
}
}
private void findViews(){
toolBar = findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
weekTitle = findViewById(R.id.weekTitle);
weekStateTxt = findViewById(R.id.weekStateTxt);
nextBtn = findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(StartProgramActivity.this, "next", Toast.LENGTH_SHORT).show();
determineWeek(1);
}
});
backBtn = findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(StartProgramActivity.this, "back", Toast.LENGTH_SHORT).show();
determineWeek(0);
}
});
setUpWeekNavigation();
recyclerView = findViewById(R.id.weekExercisesRecycler);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
for(int i = 0 ; i<7; i++){
setWeekExercisesData(i);
}
userProgramWeekAdapter = new UserProgramWeekAdapter(this,programTrackerWrappers);
recyclerView.setAdapter(userProgramWeekAdapter);
}
public void getProgramDetails(int week){
allProgramTrackers = new ArrayList<>();
programTrackerReference
.child(GlobalUser.getmUser().getiD())
.child(program.getProgramsId()).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ProgramTracker programTracker = dataSnapshot.getValue(ProgramTracker.class);
// Log.d("Progra")
if(programTracker.getWeek() == (week-1)) {
allProgramTrackers.add(programTracker);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setWeekExercisesData(int position){
switch (position){
case 0:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Monday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper = new ProgramTrackerWrapper("Monday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 1:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Tuesday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper1 = new ProgramTrackerWrapper("Tuesday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper1);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 2:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Wednesday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper2 = new ProgramTrackerWrapper("Wednesday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper2);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 3:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Thursday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper3 = new ProgramTrackerWrapper("Thursday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper3);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 4:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Friday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper4 = new ProgramTrackerWrapper("Friday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper4);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 5:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Saturday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper5 = new ProgramTrackerWrapper("Saturday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper5);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
case 6:
dayOfWeekExercises = new ArrayList<>();
for (int i = 0; i<allProgramTrackers.size(); i++){
if(allProgramTrackers.get(i).getDay().equals("Sunday")){
dayOfWeekExercises.add(allProgramTrackers.get(i));
}
}
ProgramTrackerWrapper programTrackerWrapper6 = new ProgramTrackerWrapper("Sunday", allProgramTrackers);
programTrackerWrappers.add(programTrackerWrapper6);
Log.d(TAG, "DayOfWeekExercises: " + dayOfWeekExercises.toString());
break;
}
}
public ArrayList<ProgramTracker> getDayOfWeekExercises() {
return dayOfWeekExercises;
}
}
答案 0 :(得分:1)
这看起来可疑:
for (int i = 0; i < programTrackers.size(); i++){
if ( programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()){
isFinished = true;
}
}
您检查programTrackers
列表的大小,但不检查其子列表的大小,因此i
可以很容易地指向错误的索引:
final int sublistSize = programTrackers.get(position).getProgramTrackers().size();
for (int i = 0; i < sublistSize; i++){
if (programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()){
isFinished = true;
}
}