Android随机数未从内存中清除

时间:2013-11-11 20:06:49

标签: android random spinner

我的应用程序中有一个骰子滚动微调器应该下拉并显示两个选项,根据单击的选项,它应该显示该范围内的随机数。我现在的问题是,它可能是因为onItemSelced监听器,但是当您从微调器中选择一个选项并获得随机数时,如果没有先选择其他选项,则无法选择相同的选项。例如,如果两个人想要滚动D6,在显示第一个随机数后,必须单击D20并获得一个随机数,然后才能再次选择D6。我不知道如何解决这个问题。

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // An item was selected.
     if (firstrun)
        {
            firstrun = false;
            return;
        }
        Random rand = new Random();
    int roll = rand.nextInt(DICE[spinner1.getSelectedItemPosition()])+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();
}

1 个答案:

答案 0 :(得分:0)

我认为这一行是问题

  

//选择了一个项目。

     

int roll = DICE [spinner1.getSelectedItemPosition()];

   roll = rand.nextInt(20) + 1;

未使用第一行,因为您在下一行中为“roll”指定了另一个值 如果你在DICE中保存值6和20,这将是一个解决方案

int roll = rand.nextInt(DICE[spinner1.getSelectedItemPosition()])+1;

否则这应该有用

if (spinner1.getSelectedItemPosition()==0)
{
    roll = rand.nextInt(6)+1;  // this line was corrected after a comment
}
else
{
    roll = rand.nextInt(20)+1;
}

我希望这会有所帮助 托马斯

- 第二天

嗨,我把它写下来测试了一下。这适用于我的Eclipse环境

RES /值/ strings.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">StackOverflow</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>


    <string-array name="dice">
        <item>6</item>
        <item>10</item>
        <item>20</item>
    </string-array>
</resources>

Java的类

package tbn.stackoverflow;

import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Starter extends Activity implements OnItemSelectedListener
{
    // check twice that this array and the array in res/values/string have the same order!!
    int DICE[] = {6,10,20};
    Spinner spinner1;
    boolean firstrun = true;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starter);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(this);
        fillSpinner();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.starter, menu);
        return true;
    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
    {
        if (firstrun)
        {
            firstrun = false;
            return;
        }
        Random rand = new Random();

        int roll = rand.nextInt(DICE[spinner1.getSelectedItemPosition()]) + 1;

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

        // a Textview is faster for testing
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(text);

//      // 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();
    }

    private void fillSpinner()
    {
        // Copied from http://developer.android.com/guide/topics/ui/controls/spinner.html
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
                R.array.dice, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner1.setAdapter(adapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}