如何从字符串数组中随机获取文本?

时间:2013-04-08 09:36:51

标签: android arrays loops android-arrayadapter

我希望通过使用文本字段(AutoCompleteTextView)从click事件的字符串数组中随机获取文本值。

这里的java文件:

String[] questionsOpt = { "I just ejaculated blood", "I just eat", "I just emptied my 401k", "I just exist", 
        "tattooed my face", "threw up yellow stuff", "threw up in my mouth", "took a huge dump"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_game);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, questionsOpt);

    AutoCompleteTextView actvDev = (AutoCompleteTextView) findViewById(R.id.actvDev);
    actvDev.setThreshold(1);
    actvDev.setAdapter(adapter);
}

如何添加循环以从按钮单击事件中的字符串数组中随机查找字符串值?

1 个答案:

答案 0 :(得分:9)

您可以使用Random

Random random = new Random(); // or create a static random field...
String randString = questionsOpt[random.nextInt(questionsOpt.length)];