mardi 15 mars 2016

Shuffling buttons randomly android

Working on a word quiz game. The app fetches a definition and three words through an API and then adds them to the ui (as shown in the image). 2 of these words are the wrong answer for the definition displayed in the textview.

So created a String array in the onPostExecute method to hold all the buttons and then tried to randomize over that array but the program generates the same results every-time.

Also, how would one prevent the randomizer from picking the same value twice? (i.e. 2 buttons with the same name)

 String[] buttons = new String[]{finalword,finaldummy1,finaldummy2};
            int first = random.nextInt(1);


        button1.setText(buttons[first]);
        button2.setText(buttons[1-first]);
        button3.setText(buttons[2-first]);

thanks for your time.




Aucun commentaire:

Enregistrer un commentaire