• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting Multi-Dimensional Array from Resources

Slaine82

Newbie
Hi all,

I am new to Android development and I am looking for some advice/help. I am building a quiz application with ten multiple choice questions, so they have a question stem, three radio buttons and a submit button. The way I had thought of setting up the answers to populate the radio buttons was setting up a multi-dimensional array, and pull that in from the resources:-

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="qAnswers">
<item>
<item>Q1Answer1</item>
<item>Q1Answer1</item>
<item>Q1Answer1</item>
</item>
<item>
<item>Q2Answer1</item>
<item>Q2Answer2</item>
<item>Q2Answer3</item>
</item>
</array>
</resources>

and then when I pull in this array use something like answersArray[0][0] to attain the Q1Answer1 item. Is this possible or is there any other suggestions on a better way to do this. Much appreciated.
 
I think, that get multy-dimention array from resources is impossible, because class TypedArray contains no methods, that return TypedArray values.
But you can code strings in array items and parse it. Example, in arrays:

Code:
<array name="quiz">
           <item>Question1@Variant1@Variant2@Variant3@</item>
           <item>Question2@Variant1@Variant2@Variant3@</item>
           <item>Question3@Variant1@Variant2@Variant3@</item>
</array>
Parse method (you read every item with this function, it write string data to array):
Code:
private static final String DELIMITER="@";
...
private void WriteArrayString(String str, int j){
        int i=0;
        while(true){
            try{
               Matrix[j][i]=str.substring(0,str.indexOf(DELIMITER));
               str=str.substring(str.indexOf(DELIMITER)+1);
            }
            catch(Exception e){
               return;
            }
            i++;
        }
}
Workflow:
1)Create two-dimention array Matrix
2)Obtain quiz array from resourses
3)For every string of quiz array call parse function (j-string index)
 
Sorry Dimiter, still having problems on this again any advice appreciated, so in order to populate my radio button I had the following set up

Resources:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="qAnswers">
<item>
<item>Option1@Option2@>Option</item>
</item>
</array>
</resources>

then in the code:-

String[] answers = res.getStringArray(R.array.qAnswers);
for(int a = 0; a < answers.length; a++){
WriteArrayString(answers[a], a);
}

and finally the function:-

private void WriteArrayString(String str, int j){
int i=0;
while(true){
try{
Matrix[j]=str.substring(0,str.indexOf(DELIMITER));
str=str.substring(str.indexOf(DELIMITER)+1);
}
catch(Exception e){
return;
}
i++;
}
}

It doesn't seem to make it into the while statement. Is there any better way to pull in the text and populate the radiobuttons?

Thanks again.
 
Back
Top Bottom