Tuesday, February 11, 2014

How to set the button's id through the loop array?


            
Original problem from the CSDN quiz channels, more solutions, please see: http://ask.csdn.net/ questions/1415


The original problem description:
5 button by setting the loop id, each randomly assigned.

Randomized no problem.

An error occurred in the use of two methods, random id and set the text button to set the time.

Makes a lot of confusion does not know where the problem is it?

 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 buttonId = new ArrayList (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));

}
}

}


Solution:

I think your cycle x <= buttons.length should be x
You can use the xml preset button:
 

Then later in the code, random text:
 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));

Reply:
Etc., learn
Reply:
Yes, learning to learn.
Reply:
Learn .........
Reply:
A Sasa's
Reply:
Promising ~ ~ Learning Ha
Reply:

Reply:

Reply:
The reply was deleted administrator at 2013-02-23 09:03:19

Reply:
cited a floor ojarofLove reply:
etc., learn


The answer lies in the bottom, or click the link to view the top
Reply:
Seems difficult. . . . .
Reply:
very good landlord
Reply:
Android-based, it is practical
Reply:
Agreed to set the id XML method can be directly used ID check button, if you use the buttons, can be marked on the list, with a way to take the list of elements taken button, but more trouble, suggested list to hashmap
Reply:
The reply was deleted administrator at 2013-02-22 08:51:15

Reply:
reference to the 13th floor gzl4061915 reply:
agreed to set the id in the XML method can be directly used ID check button, if you use the buttons, can be done on the list Mark, take the list with elements of methods to get button, but more trouble, suggested list to hashmap


Below already have solutions, oh
Reply:
The reply was deleted administrator at 2013-02-23 09:03:33

Reply:
The reply was deleted administrator at 2013-02-23 09:02:52

Reply:
The reply was deleted administrator at 2013-02-23 09:03:39

Reply:
Mark, the helpful way.

No comments:

Post a Comment