3

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.

zaid
  • 6,259
  • 7
  • 26
  • 20
  • 1
    R.string.qo is simply used as an integer to point to your string q0, and by putting brackets after this you are trying to call it as a function and passing a random number to it. This will fail as you cannot treat a static integer in this way. I can understand what you are trying to do however this is not the way to do it, I would suggest learning some of the basics of java. I have answered this question below using string arrays, it should be pretty clear and quite an elegant solution to your problem. If it doesn't make sense just ask. – stealthcopter Apr 26 '10 at 23:39

2 Answers2

21

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
  • 13,964
  • 13
  • 65
  • 83
  • 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 Apr 26 '10 at 23:59
  • I've edited this question to include an example of the code you should use. – stealthcopter Apr 27 '10 at 09:20
-2

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.

Nate
  • 2,462
  • 1
  • 20
  • 28
  • 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 Apr 26 '10 at 23:28