I want to move backwards in an array-list when i press a button (previous), I already did how to move next, but the previous button crashes at a point. I want it to move backwards in an array list from a point where the next button left and then stop at the 0 element.
I already tried doing this in the code below:
package com.learn.vocabularyapp;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Colors extends AppCompatActivity {
Context context= this;
String encolor;
int iteratoren;
int iteratorar;
String arcolor;
final String MYPRG = "myprg";
int prgbar = 50;
int pbOption = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colors);
final ProgressBar myprg = (ProgressBar) findViewById(R.id.myprg);
final Drawable setimg = getResources().getDrawable(R.drawable.tick);
final ImageView done = (ImageView) findViewById(R.id.done);
final String[] colorsen = context.getResources().getStringArray(R.array.colorsen);
final TextView coloren = (TextView) findViewById(R.id.colorsen);
final TextView colorar = (TextView) findViewById(R.id.colorsar);
final String[] colorsar = context.getResources().getStringArray(R.array.colorsar);
for (int i = 0; i < colorsen.length; i ++) {
encolor = colorsen[i];
}
for (int i = 0; i < colorsar.length; i ++ ) {
arcolor = colorsar[i];
}
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
encolor = colorsen[iteratoren];
arcolor = colorsar[iteratorar];
coloren.setText(encolor);
colorar.setText(arcolor);
iteratorar++;
iteratoren++;
if (iteratorar >= colorsar.length) {
iteratorar = 0;
}
if (iteratoren >= colorsen.length) {
iteratoren = 0;
}
myprg.setProgress(myprg.getProgress()+20/3);
if (myprg.getProgress() == 100) {
done.setImageDrawable(setimg);
}
}
});
Button previous = (Button) findViewById(R.id.prev);
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
encolor = colorsen[iteratoren];
arcolor = colorsar[iteratorar];
coloren.setText(encolor);
colorar.setText(arcolor);
iteratorar--;
iteratoren--;
if (iteratorar >= colorsar.length) {
iteratorar = 14;
}
if (iteratoren >= colorsen.length) {
iteratoren = 14;
}
myprg.setProgress(myprg.getProgress()-20/3);
done.setImageDrawable(null);
}
});
strings.xml file:
<string-array name="colorsen">
<item>Red</item>
<item>Orange</item>
<item>Yellow</item>
<item>Green</item>
<item> Blue</item>
<item> Purple</item>
<item>Brown</item>
<item> Magenta</item>
<item> Tan</item>
<item> Cyan</item>
<item> Maroon</item>
<item> Silver</item>
<item> Pink</item>
<item>Black</item>
<item> White</item>
<item>Gold</item>
</string-array>
<string-array name="colorsar">
<item>أحمر</item>
<item>البرتقالي</item>
<item>الأصفر</item>
<item>أخضر</item>
<item>بَنَفْسَجي</item>
<item>بنى</item>
<item>أرجواني</item>
<item>سُمْرة</item>
<item>السماوي</item>
<item>كَسْتَنائيّ</item>
<item>لَوْن فِضّيّ</item>
<item>زهري</item>
<item>أسود</item>
<item>أبيض</item>
<item>لون ذهبي</item>
</string-array>
I am a beginner in android and java so a detailed answer would be appreciated.