tags:

views:

399

answers:

2

i am trying to display a random string each time a button is pressed from a set of strings defined in strings.xml . this is an example of the strings ID's

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

java code for getting 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 an error at "q0" in R.string.q0 which is 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 it wont 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.

A: 

Why do you need this?

R.string.q0

Assuming getString(RandomQ) is a valid statement, I would think

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

would work just fine.

Also, as a side note: A lot of times those autofixes in your IDE are unreliable and unsafe to use. Unless you know why it's telling you to do something, don't do it.

Nathan
This will not work as that will just try and get a string from a random number as opposed to references one of the 5 strings.
stealthcopter
+3  A: 

You can define your strings in an array which will help simplify this task (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> 

You can then create an array to hold the strings and select a random string from the array to use:

private String[] myString; 

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

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

Example 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);
}
}
stealthcopter
thanks for the quick reply. i did as you said but i get a syntax error at the end of the first "myString;". the error is "Syntax error on token ";", , expected". changing ; to , dose not help.
zaid
I've edited this question to include an example of the code you should use.
stealthcopter
thank you very much for the helping out a noob.
zaid
No problem, hope it helps.
stealthcopter