The xml file is preprocessed into java source R.java which is then built
into your project. If you look at the source (in the gen folder) for
this you will see that R.string.q0 is a public static final int, i.e. a
named constant if you will, and that the value of the ints are in
ascending numerical order matching the order they are defined in the xml
file. That is, the value of R.string.q0a1 will be 1 more than
R.string.q0 (going by your xml). If your xml has a defined pattern such
that there is always the same number of strings between similar entries,
e.g. q1 is 7 after q0 and q2 is 7 after q1, you can use this to find a
random question e.g.
int randomQ = R.string.q0 + (rGenerator.nextInt(204) * 7);
String Question = getString(randomQ);
String Answer1 = getString(randomQ +1);
etc.
Regards, Chris
On 20/03/2010 05:36, Ben Orchard wrote:
I have a strings.xml file that takes the following format (in general):
<string name="q0"></string>
<string name="q0a1"></string>
<string name="q0a2"></string>
<string name="q0a3"></string>
<string name="q0a4"></string>
<string name="q0KEY">1</string>
<string name="q0ANS">ans-a01</string>
There are 200+ questions and response sets. I am trying to select a
random one, (say q183) and then use the text in a text view like such:
Random rGenerator = new Random();
int randomQ = rGenerator.nextInt(204);
String Q_ID = 'q' + getString(randomQ);
List<Integer> intlist = new ArrayList<Integer>();
intlist.add(new Integer(1));
intlist.add(new Integer(2));
intlist.add(new Integer(3));
intlist.add(new Integer(4));
Collections.shuffle(intlist);
String ANS_ID1 = "R.string."+Q_ID + "a"+getString(intlist.get(0));
String ANS_ID2 = "R.string."+Q_ID + "a"+getString(intlist.get(1));
String ANS_ID3 = "R.string."+Q_ID + "a"+getString(intlist.get(2));
String ANS_ID4 = "R.string."+Q_ID + "a"+getString(intlist.get(3));
String Question = getString(R.string.Q_ID); //this is
considedered a syntax error by java/android
TextView QuestionView = (TextView)
findViewById(R.id.QuestionText);
QuestionView.setText(Question);
No matter how I construct (at least, as I can think of it) the text of
the string to pass as a parameter to the value of Question, I end up
with problems.
At this point I absolutely need to be able to choose an arbitrary
string from strings.xml, with the specific set of strings to follow.
Any help would be appreciated. Yes I know a database *might* be
simpler, but I cannot figure out the proper code to make this work
either. The ONE tutorial on the subject leaves out critical
information. I really need help on this.
Thanks
Ben
--
I have no bit of wisdom to include as a signature.
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
To unsubscribe from this group, send email to
android-beginners+unsubscribegooglegroups.com or reply to this email
with the words "REMOVE ME" as the subject.
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
To unsubscribe from this group, send email to
android-beginners+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.