I'm using two buttons to iterate back and forth in an arraylist that contains 12 months, when i reach the last element and press the prevButton the app crashes, this also happens when i go back to the first element and hit the nextButton, how do i fix my if statment?
I have tried to do it with >= and <= but it didn't work
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i < months.size()) {
Month month = months.get(i);
monthTextView.setText("" + month);
i++;
}
}
});
prevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i >= 0) {
Month month = months.get(i);
monthTextView.setText("" + month);
i--;
}
}
});
i- so to say,iis pointing to the next value - do as 'm.antkowicz' wrote and increment/decrement before getting themonth- and a little note: addingmonthto an empty string is a bit weird, use...setText(month.toString())