0

I have the following code lines. I need to check each button (there are 4: a,b,c,d) and compare if the answer is right. If it is, I say that the button set its color to green.

But I need to reduce this code, with a iterator or something like that, because if increase the quantity of buttons will be more complicated.

if(currentQuestion.getOptA().equals(currentQuestion.getAnswer()))
{
    buttonA.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
} else if(currentQuestion.getOptB().equals(currentQuestion.getAnswer()))
{
    buttonB.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
} else if(currentQuestion.getOptC().equals(currentQuestion.getAnswer()))
{
    buttonC.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
} else if(currentQuestion.getOptD().equals(currentQuestion.getAnswer()))
{
    buttonD.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Camilo Andres
  • 196
  • 1
  • 12
  • Have a look at arrays and loops. – Henry Feb 23 '18 at 12:32
  • use switch case. – Lingeshwaran Feb 23 '18 at 12:37
  • Possible duplicate of [How to iterate through a view's elements](https://stackoverflow.com/questions/4809834/how-to-iterate-through-a-views-elements) – Adinia Feb 23 '18 at 12:41
  • I do not wanna write a long answer but I think you can reduce code and complexity by using switch for each button and implement your background there this way it will savethe remaining if checks. . . – Adam Feb 23 '18 at 12:46

4 Answers4

2

The general idea is:

1) get the count of the buttons

2) make an array and put inside the dynamic portion of the var you want to check e.g. a,b,c,d

3) use a for to iterate from 0 to count of buttons

4) compare each time the name by concatenating the static portion of the name plus the dynamic portion of the array e.g. getOpt is the static and the a, b c, d is the dynamic portion of the name

So, with this "technique" you will be able to scale your checks with zero effort just by adding a new value to the array with a,b,c,d

AlexCode
  • 655
  • 5
  • 18
1
Object possibleAnswers [] = {currentQuestion.getOptA(), currentQuestion.getOptB(), currentQuestion.getOptC(), currentQuestion.getOptD()};
Button buttons [] = {buttonA, buttonB, buttonC, buttonD};

for (int i = 0; i < possibleAnswers.lenght; i++) {
    if (currentQuestion.getAnswer().equals(possibleAnswers[i]) {
        buttons[i].setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        break;
    }
}

You can also create an method in Question class to return the array of choices... Something like:

public Object [] getPossibleAnswers() {
    return new Object[] {currentQuestion.getOptA(), currentQuestion.getOptB(), currentQuestion.getOptC(), currentQuestion.getOptD()};
}

And then,

Object possibleAnswers [] = currentQuestion.getPossibleAnswers();

NOTE

I used Object because I don't know your class's name.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
0

Since you seem to be checking one thing and setting another (which correspond to each other), you should go for a HashMap.

You would then iterate over the map (which stores a key and value combo) and if the key matches the right answer, then you use the value to set the background colour.

public HashMap<String, JButton> getQuestionAndAnswers() {
     HashMap<String, JButton> result = new HashMap<>();
     result.put(currentQuestion.getOptA(), buttonA);
     ....
}

And then you could use it like so:

HashMap<String, JButton> qaa = getQuestionAndAnswers();
foreach(String key in qaa) {
     if(key.equals(currentQuestion.getAnswer()) {            qaa.get(key).setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
     break;
     }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
0

To ensure the following way I suggest that you should use RadioGroup and also use a selector for each radioButton and set unchecked and checked states for that.

you most create a drawable shape like this:

<item android:state_checked="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="@color/lightGreen"
      />


        <corners android:radius="2dp" />
    </shape>
</item>

<item android:state_checked="false">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="@color/yourOffColor"
            />
    </shape>

</item>

and use this in java when the ans in true set her button checked like this: :

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int radioID = rg.getCheckedRadioButtonId();
            int ansID= 2; //replace your id

            radioBtn = (RadioButton) findViewById(radioID);

            if (radioID == ansID){
            radioBtn.setChecked(true);
        }}
    });

this is a way that i using in my app perhaps useful for you

Aslami.dev
  • 880
  • 8
  • 19