Show random line

I am trying to display a random string on every button click from a set of strings defined in strings.xml. this is an example of row ids

<string name="q0">
    <string name="q1">
    <string name="q2">
    <string name="q3">
    <string name="q4">

      

java code to get a random string.

private static final Random rgenerator = null;

    int RandomQ = R.string.q0  (rgenerator.nextInt(5) + 1);
    String q = getString(RandomQ);

      

if i try to use this java code i get a "q0" error in R.string.q0 which The method q0(int) is undefined for the type R.string

if i try to use the quick fix and create a method it works. but that won't let me save or run the app because it replaces my create method and shows this message

R.java was modified manually! Reverting to generated version!

      

Thanks for reading.

+2


a source to share


3 answers


You can define your strings in an array, which will make this easier (res / values โ€‹โ€‹/array.xml):

<string-array name="myArray"> 
    <item>string 1</item> 
    <item>string 2</item> 
    <item>string 3</item> 
    <item>string 4</item> 
    <item>string 5</item>
</string-array> 

      

Then you can create an array to store the strings and pick a random string from the array used:



private String[] myString; 

myString = res.getStringArray(R.array.myArray); 

String q = myString[rgenerator.nextInt(myString.length)];

      

Sample code :

package com.test.test200;

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {
/** Called when the activity is first created. */

    private String[] myString;
    private static final Random rgenerator = new Random();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Resources res = getResources();

    myString = res.getStringArray(R.array.myArray); 

    String q = myString[rgenerator.nextInt(myString.length)];

    TextView tv = (TextView) findViewById(R.id.text1);
    tv.setText(q);
}
}

      

+21


a source


create a string array in res / values โ€‹โ€‹/array.xml:

<?xml version="1.0" encoding="utf-8"?>  
    <resources>  
<string-array name="custom_array"> 
    <item>aalia</item> 
    <item>asin</item> 
    <item>sonakshi</item> 
    <item>kajol</item> 
    <item>madhuri</item>
</string-array> 
</resources>

      



then in your activity write the code

String[] array = context.getResources().getStringArray(R.array.custom_array);

        String randomStr = array[new Random().nextInt(array.length)];

        text_dialog.setText(""+randomStr);

      

+1


a source


Why do you need this?

R.string.q0

Assuming that getString(RandomQ)

is a valid statement, I think

int RandomQ = rgenerator.nextInt(5) + 1;

      

will work fine.

Also, as a side note, many times these auto-fixes in your IDE are unreliable and insecure. If you don't know why he tells you to do something, don't do it.

-2


a source







All Articles