0

I am trying to set the id's of 5 (five) buttons programatically and shuffle them every time.
There's no problem with shuffle.

I get errors when I use 2 (two) methods that shuffles and set id of 5 buttons AND set the texts of each one.
I know this could be a mess because it's my first time to try for-each loop.
Please help. Thank you.

Button b1, b2, b3, b4, b5;
Button[] buttons = { b1, b2, b3, b4, b5 };

    public void shuffleButtons() {

            Integer[] Id = { R.id.bChoice1, R.id.bChoice2, R.id.bChoice3,
                    R.id.bChoice4, R.id.bChoice5 };

            ArrayList<Integer> buttonId = new ArrayList<Integer>(Arrays.asList(Id));

            Collections.shuffle(buttonId);


                for (int x = 0; x < 5; x++) {

                    for (Button b : buttons) {

                        b = (Button) findViewById(buttonId.get(x));

                    }

                }


        }

public void setButtonTxt() {


            for (Button b : buttons) {

                for (int x = 0; x <= buttons.length; x++) {

                    b.setText(textList.get(x));

                }
            }

    }
1
  • Why not use a listView instead? It might be a better fit for this. Commented Feb 18, 2013 at 20:50

3 Answers 3

2

I am trying to set the id's of 5 (five) buttons programatically and shuffle them every time. There's no problem with shuffle.

My suggestion is that you shoudn't, musn't do it. Each id should be created from XML and then each is automatic generated in R.java as static int field.

You should respect this rule and don't create "spaghetti code".

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

Comments

0

In short, I'm guessing your issue is x <= buttons.length should be x < buttons.length in your for loop. However it looks like you're going to set all 5 buttons to textList.get(5) with the current code you have.

Why not try with preset buttons in XML?

<Button android:id="@+id/bChoice1" ... />
<!-- etc -->
<Button android:id="@+id/bChoice5" ... />

Then in the code behind, randomize the text instead of both?

Button b1 = (Button)findViewById(R.id.bChoice1);
Button b2 = (Button)findViewById(R.id.bChoice2);
Button b3 = (Button)findViewById(R.id.bChoice3);
Button b4 = (Button)findViewById(R.id.bChoice4);
Button b5 = (Button)findViewById(R.id.bChoice5);

// assuming textList is an ArrayList of text items
// the next two lines will randomize your textList order
// so you don't need to do it yourself and much less
// error-prone
long seed = System.nanoTime();
Collections.shuffle(textList, new Random(seed));

b1.setText(textList.get(0));
b2.setText(textList.get(1));
b3.setText(textList.get(2));
b4.setText(textList.get(3));
b5.setText(textList.get(4));

2 Comments

okay. i'll try that. thanks! BTW, i didn't get that 'long' variable along with the collections. those two lines of codes.
Added a comment in. It's basically randomizing the order of the textList ArrayList
0

it's better for you to write your code in this way :

Button [] buttens=new Button[5];
final int ID= R.id.bChoice1;
for (int i = 0; i < 5; i++) {
        button[i]=(Button)findViewById(ID+i);
    }

but be careful it works as long as you set your button id's consecutively !

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.