lukaszgo3
lukaszgo3

Reputation: 149

ArrayList is picking randomly

everyone, I'm just a beginner so please be patient ;) I have an ArrayList and it's picked randomly to my cardView, what should I change if I want pick it one by one in order?

 public static class Questions {
        private String mQuestion;
        private String mAnswer;


        // statyczne tablice, na podstawie których zostaną uzupełnione obiekty artykułów
        private static String[] sQuestions = {"1 Lorem ipsum dolor sit amet",
                "2 Etiam sit", "3 Cras vel lorem",
                "Cras suscipit, urna at aliquam rhoncus",
                "Phasellus congue lacus eget neque",
                "Phasellus pharetra nulla ac diam"};


        private static String[] sAnswers = {"1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam.",
                "2 Quisque semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed posuere libero dui id orci",
                "3 Nam congue, pede vitae dapibus aliquet, elit magna vulputate arcu, vel tempus metus leo non est. Etiam sit amet lectus quis est congue mollis.",
                "Phasellus congue lacus eget neque. Phasellus ornare, ante vitae consectetuer consequat, purus sapien ultricies dolor, et mollis pede metus eget nisi.",
                "Praesent sodales velit quis augue. Cras suscipit, urna at aliquam rhoncus, urna quam viverra nisi, in interdum massa nibh nec erat."};

        public Questions() {
            Random arry = new Random();

            mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
            mAnswer = sAnswers[arry.nextInt(sAnswers.length)];
        }

        public String getTitle() {

            return mQuestion;
        }

        public String getContent() {
            return mAnswer;
        }
    }

Upvotes: 0

Views: 104

Answers (5)

T.Dimitrov
T.Dimitrov

Reputation: 157

Your array returns randomly because you have written it in this way. First, take a look at your code

        Random arry = new Random();
        mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
        mAnswer = sAnswers[arry.nextInt(sAnswers.length)];

You are taking a random element of your arrays because of the random function. What you need to do is to replace the index part aka this one [arry.nextInt(sQuestions.length)]with the index of the element you want. If you want the first -> 0, 2nd -> 1 and so on. Don't go over arr.length-1 or an exception will be thrown.

All in all, put the indexes of the items you want to get like this,for example.:

    mQuestion = sQuestions[0];
    mAnswer = sAnswers[0];

The getters will give you "1 Lorem ipsum dolor sit amet" and "1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam."

Make counters, create your own logic for how to return elements when the functions are called.

Upvotes: 1

Berkley Lamb
Berkley Lamb

Reputation: 293

Why don't you add a global variable as the max value of the array? then you can iterate it everytime Questions is called.

if(questionPlace < max){
 //assign values, questionPlace++;
}else{
 questionPlace = 0;
 //assign values
}

Upvotes: 0

aneurinc
aneurinc

Reputation: 1238

To iterate through an array in order you can do the following:

for (int i = 0; i < array.length; i++) {
    String s = array[i];
    // Do what you want with your string s
}

Upvotes: 0

Chris Sharp
Chris Sharp

Reputation: 1995

These lines need to be changed to point to desired indexes.

 mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
 mAnswer = sAnswers[arry.nextInt(sAnswers.length)];

For example, if you wanted the question and answer at 5 then you would need to write

mQuestion = sQuestions[4];
mAnswer = sAnswers[4];

How you get the number (4 in my example) is your choice. A for loop or a while loop with incrementation will work. Passing direct numbers from keyboard input would also work.

Upvotes: 1

t4u
t4u

Reputation: 178

Is that what you mean?

int a=0;
while (a<arry.length)
{
        mQuestion = sQuestions[a];
        mAnswer = sAnswers[a];
   a++;
}

Upvotes: 0

Related Questions