samedi 3 octobre 2015

How to Produce Random UPPERCASE English Alphabets in Android and Set in Button Text

I am making a University Task to generate a random UPPERCASE Alphabets and then setText in button that i have made

      final int[] _btn_no = {R.id.btna_1, R.id.btna_2, R.id.btna_3, R.id.btna_4, R.id.btna_5, R.id.btna_6, R.id.btna_7, R.id.btna_8, R.id.btna9, R.id.btna_10, R.id.btna_11, R.id.btna_12, R.id.btna_13, R.id.btna_14, R.id.btna_15, R.id.btna_16, R.id.btna_17, R.id.btna_18, R.id.btna_19, R.id.btna_20,R.id.btna_21, R.id.btna_22, R.id.btna_23, R.id.btna_24, R.id.btna_25, R.id.btna_26};
    String Alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random alpha_ran;
    Button _random_buttons;
    alpha_ran = new Random();
                List<Character> generated = new ArrayList<Character>();


                for (int i = 0; i <= 26; i++) {

                    int next = alpha_ran.nextInt(26);

                    while (true)

                    {

                        if (!generated.contains(next)) {
                            // Alphabets.charAt(alpha_ran.nextInt(Alphabets.length()));
                            generated.add(Alphabets.charAt(alpha_ran.nextInt(Alphabets.length())));

                            Log.d("", generated.toString());
                            _random_buttons = (Button) findViewById(_btn_no[i]);

//                          _random_buttons.setText(next);

                            break;


                        }

                    }
                }

Exception:    java.lang.ArrayIndexOutOfBoundsException: length=26; index=26 at _random_buttons = (Button) findViewById(_btn_no[i]);

Can any one Tell me how to resolve this issue?




Aucun commentaire:

Enregistrer un commentaire