Wannabe
Wannabe

Reputation: 737

Shorter way to code

I recently started programming Android. I'm not that good jet. I started to develop an application for myself to learn Android. As I'm new to developing I was wondering if there is a (maybe nicer) way to code this.

This piece of code gets the answer to a question from a array. If the current question is the first question it will get the answers for the first question and load them in radiobuttons.

if (currentQuestion == 0) {
        arr = Arrays.asList(getResources().getStringArray(R.array.question1));
    } else if (currentQuestion == 1) {
        arr = Arrays.asList(getResources().getStringArray(R.array.question2));
    } else if (currentQuestion == 2) {
        arr = Arrays.asList(getResources().getStringArray(R.array.question3));
    } else if (currentQuestion == 3) {
        arr = Arrays.asList(getResources().getStringArray(R.array.question4));
    } else if (currentQuestion == 4) {
        arr = Arrays.asList(getResources().getStringArray(R.array.question5));
    }

All help is appreciated. Thanks in advance!!

Upvotes: 0

Views: 69

Answers (1)

sdabet
sdabet

Reputation: 18670

You could store the indexes in an array:

int[] questions = { 
    R.array.question1, 
    R.array.question2, 
    R.array.question3,         
    R.array.question4, 
    R.array.question5 
};

arr = Arrays.asList(getResources().getStringArray(questions[currentQuestion));

Upvotes: 7

Related Questions