0

How can I change this coding into an array? Because I found this step is too long. I'm new to this so can you help me. Thank you.

if(num2 == 1)
    {
        display.setImageResource(R.drawable.one);
        if (num1 == 1)
        {
            display2.setImageResource(R.drawable.one);
        }
        else if (num1 == 2)
        {
            display2.setImageResource(R.drawable.two);
        }
        else if (num1 == 3)
        {
            display2.setImageResource(R.drawable.three);
        }
        else if (num1 == 4)
        {
            display2.setImageResource(R.drawable.four);
        }
        else if (num1 == 5)
        {
            display2.setImageResource(R.drawable.five);
        }
    }
5
  • This code works, so why bother? It's fast enough and gives you the right result. "Don't fix it if it ain't broken". Commented Dec 4, 2013 at 15:10
  • 2
    On a more relevant note, codereview.stackexchange.com Commented Dec 4, 2013 at 15:10
  • Can't you store an R.drawable... type rather than num1? Commented Dec 4, 2013 at 15:15
  • @AleksG thank you sir. But the problem is that I have to code both the variables num2 and num1 until number 10. So won't it be long sir? Commented Dec 4, 2013 at 15:20
  • 1
    A switch statement would be slightly cleaner, but I'm not sure the long code is unavoidable. Commented Dec 4, 2013 at 15:21

2 Answers 2

2

If you have control over your R object, I'd strongly advise to simply store the drawable members as an array to begin with. But if you insist,

Drawable[] drawables = new Drawable[...];
drawables[1] = R.drawable.one;
drawables[2] = R.drawable.two;
...
if(num2 == 1) {
    int index = (num1 >= 1 && num1 < drawables.length) ? num1 : 1;
    display2.setImageResource(drawables[index]);
}

Although it may be better to just stick to your current code or use switch. It's verbose, yes, but pretty clear.

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

2 Comments

Thanks sir. But this is only the part of the coding. I will have to code this till number 10 for each variable. So won't it be long?
@amln_ndh You mean the initialization? How would you avoid it when they're class members and must be explicitly named to be accessed?
0

One option would be to use a LevelListDrawable.

You define the Drawable like so:

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="1" android:drawable="@drawable/one />
    <item android:maxLevel="2" android:drawable="@drawable/two" />
    <item android:maxLevel="3" android:drawable="@drawable/three" />
    <!-- ... -->
</level-list>

Either in XML or in your code, set the background of display2 to this Drawable.

You can then simplify your code to:

if(num2 == 1) {
    display2.setImageLevel(num1);
}

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.