0

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.

2 Answers 2

1

According to your logic, you could change the code:

if (iteratorar >= colorsar.length) {
    iteratorar = 14;
}
if (iteratoren >= colorsen.length) {
    iteratoren = 14;
}

to:

if (iteratorar < 0) {
    iteratorar = 0;
}
if (iteratoren < 0) {
    iteratoren = 0;
}

This would prevent it from crashing when you get to the leftmost item and press previous.

Sign up to request clarification or add additional context in comments.

Comments

0

you need to make sure that you stop at index 0. having iteratorar--; and iteratoren--; will cause the index to fall to -1 and crashes the app. So, you can do one of 2 things:

  1. checking the values of iteratorar--; and iteratoren--; to make sure that you don't reach/use -1. some thing like if(iteratorar == 0) {return;}
  2. once the index reaches 0, you set the values of iteratorar--; and iteratoren--; to the (sizeOfTheArray - 1), which will allow you rotate from the end again.

Hope you get the idea

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.