2014-01-20 42 views
-1

串什麼,我已經學會爲止:Android的填充陣列從string.xml

在strings.xml中我有一堆

<string name="q1">what is the name of blah blah</string> 
<string name="q2">what is the name of blah blah</string> 
<string name="q3">what is the name of blah blah</string> 
<string name="q4">what is the name of blah blah</string> 

,並在主類我要創建數組問題對象來訪問它們 整個主類像

private Question[] mQuestionList = new Question[]{ 
    new Question(R.string.q1, true), 
    new Question(R.string.q2, false), 
    new Question(R.string.q3, true), 
    new ... 
} 

什麼IM好奇的是,如果我有200個問題,我知道我有stirng他們每個人寫出來的。 xml文件,但是我認爲在主類中200「新問題(R.string.qx,true/false)」語句不是正確的方法。我相信有更多簡單的方法來填充mQuestionList數組。如果有什麼?

回答

4

你可以把這個價值觀/ arrays.xml

<string-array name="array_name"> 
    <item>1</item> 
    <item>2</item> 
    <item>3</item> 
    <item>4</item> 
    <item>5</item> 
    <item>6</item> 
    <item>7</item> 
</string-array> 

然後做

String[] questionStrings = getResources().getStringArray(R.arrays.array_name); 
mQuestionList = new Question[questionStrings.length]; 

for (int i = 0; i < questionStrings.length; i++) { 
    mQuestionList[i] = new Question(questionStrings[i], ... 

如果第二個參數,布爾之一,每題不同,它們存儲的新values/arrays.xml中的數組