2017-05-25 78 views
0

我目前正在开发android 测验应用程序,我实现了“重置”按钮,可以重置问题并随机化它们,但是我' m在调用MainActivity.java中的.xml文件中的随机字符串时遇到了麻烦。Java/Xml/Android - 从Java中调用随机字符串String文件

我都在像这样的strings.xml文件中列出了我的问题,以使它们易于翻译:

<string name="q1">The reactor at the site of the Chernobyl nuclear disaster is now in which country?</string> 
<string name="a1">A. Slovakia</string> 
<string name="b1">B. Ukraine</string> 
<string name="c1">C. Hungary</string> 
<string name="d1">D. Russia</string> 
<string name="q1answer">B</string> 

所有的问题在该文件中列出像Q1, q2,q3,和ABCD答案相同:a1,b1,c1,d1; a2,b2,c2,d2等。

有很多问题,但按钮只选择其中5个并将它们显示在屏幕上。该问题是,我只是不能访问字符串,如果我想使用随机生成一个整数可能发现它们的.xml

for (int i = 1; i <= 5; i++) { 

     // I managed to get the identifier of the TextView with the for loop like that (TextViews for questios are listed q1q, q2q, q3q, q4q, q5q): 
     // I would like to do the same thing down there with Strings. 
     TextView question = (TextView) findViewById(getResources().getIdentifier("q" + i + "q", "id", this.getPackageName())); 

     // randomQuestion is the number of the question in random number generator from different method. 
     randomQuestion = randomizeNumbers(); 

     // And here I'm stuck, this will show "q1-randomNumber" instead of the real question, 
     // because it does not see it as an ID. I tried various different solutions, but nothing works. 
     question.setText("q" + randomQuestion); 

     // I left the most silly approach to show what I mean. 
    } 

我能做些什么,以使计算机区分名的字符串?所以它显示的是真正的问题而不是“q1”“q6”“q17”?

感谢您提前帮忙!

回答

0

您可以使用getIdentifier()实用程序从string.xml文件中访问字符串。

private String getStringByName(String name) { 
    int resId = getResources().getIdentifier(name, "string", getPackageName()); 
    return getString(resId); 
} 

这将是一个容易得多,如果你还试图完全另一种方法,并创建一个问题类,它看起来是这样的:

public class Problem(){ 
int ID; 
String question; 
String answer1; String answer2; //..and so on 
String answerCorrect; 

public class Problem(int ID, String question, ...){ 
this.question= question; 
... 
} 
} 

而且比,创建和ArrayList =新的ArrayList <>( );并填写你的问题。相比之下,您可以通过数组中的ID /位置访问它们,按名称搜索它们等等。将来,您可以使用此模型从服务器请求问题列表。

+0

可悲的是我被告知使用的.xml所以它是翻译之后轻松,但第一个回复解决问题 :)。 (); getIdentifier(“q”+ randomQuestion,“string”,this.getPackageName()); 完美的作品,早些时候我没有将“id”改为“string”。谢了哥们! – Qlak

+0

很高兴帮助!试图给一点额外的东西:如果解决了问题,D可以自由地“回答”答案。祝你好运 ! –

0
question.setText("q" + randomQuestion); 

在这里,因为你通过你的"q"参数推断为一个字符串,所以它会显示"q-randomNumber"预期。我想你想要的是通过和实际的ID到setText。要做到这一点,你可以采用与使用“string”而不是“id”查找问题textview ID相同的方法。

+0

那是真的,早些时候我没有把“id”改成“string”。现在它工作得很好,欢呼! ()。getIdentifier(“q”+ randomQuestion,“string”,this.getPackageName()); – Qlak

+0

你介意,如果我问你这是否正确的答案,如果它帮助你?这对我来说是很大的。 :) – tompee

0

在MainActivity或功能:

Resources res = getResources(); 

     myString = res.getStringArray(R.array.YOURXMLFILE); 

     String q = myString[rgenerator.nextInt(myString.length)]; 
// WE GET THE TEXTVIEW 
     tv = (TextView) findViewById(R.id.textView15); 
     tv.setText(q); 
// WE SET THE TEXTVIEW WITH A RANDOM QUESTION 

,并声明:

private String[] myString; 
private static final Random rgenerator = new Random(); 
private TextView tv;