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.

R.string is a Java class, specifically an inner class of the R class,
generated for you by Android. You can't just invent new fields on that
(Q_ID), any more than you can for any other Java code.

> 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.

Resources has getIdentifier() which can convert the string form of a
resource identifier into a number. This uses reflection and is much more
expensive than just using the public static data member on R.string.
Hence, if you use this, please cache your results.

> 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.  

Here are some sample database projects from some books written by some
balding guy:

http://github.com/commonsguy/cw-android/tree/master/Database/Constants/
http://github.com/commonsguy/cw-andtutorials/tree/master/11-Database/

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Training...At Your Office: http://commonsware.com/training

-- 
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.

Reply via email to