Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text randomly from the string array?

I want to get text value randomly values from the string array on the click event, with the using the Text Field (AutoCompleteTextView).

java file here:

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

How to add here loop for randomly find string value from the string array on button click event??

like image 951
Engr Waseem Arain Avatar asked Jun 06 '26 09:06

Engr Waseem Arain


1 Answers

You can use Random:

Random random = new Random(); // or create a static random field...
String randString = questionsOpt[random.nextInt(questionsOpt.length)];
like image 75
MByD Avatar answered Jun 07 '26 23:06

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!