selected option not clearing from memory android

Posted by user2980560 on Stack Overflow See other posts from Stack Overflow or by user2980560
Published on 2013-11-12T03:50:21Z Indexed on 2013/11/12 3:53 UTC
Read the original article Hit count: 109

Filed under:
|
|

I have a small random number spinner that when you click gives a random number. I am having two problems. The first is when the main activity loads it displays a random number on the screen without the random number spinner being clicked. I am unsure what to set to false to keep it from opening with the main activity. The second problem is that when you select an option from the spinner it does not clear. Meaning that If you click on option D6 or D20 then you can not click on the same option again until selecting the other option first. Essentially the selection does not clear out of memory after the random number is selected. Here is the random number code

 public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    Random rand = new Random();
    int roll;
    // An item was selected.
    if (spinner1.getSelectedItemPosition()==0)
    {
        roll = rand.nextInt(6)+1;
    }
    else
    {
        roll = rand.nextInt(20)+1;
    }

    // Put the result into a string.
    String text = "You rolled a " + roll;

    // Build a dialog box and with the result string and a single button
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(text).setCancelable(false)
            .setPositiveButton("OK", new 
 DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id)  
 {
                    // do things when the user clicks ok.
                }
            });
    AlertDialog alert = builder.create();

    // Show the dialog box.
    alert.show();
}

© Stack Overflow or respective owner

Related posts about android

Related posts about random