如何从字符串数组中提取名称属性?
您好,我构建了一个测验应用程序。 我有以下 (values/)question.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="question">
<item name="correct">A</item>
<item name="wrong">B</item>
<item name="wrong">C</item>
<item name="wrong">D</item>
</string-array>
</resources>
我想要一个有四个可能答案的问题,但是当我在 Java 中检索答案时..我不知道哪个答案是正确的。因此,我决定在项目标签中使用名称属性来传递“正确”或“错误”答案的值。 有没有办法获取名称和标签值?
因为当我使用 String[] test = res.getStringArray(R.array.question);我只能获取数组中每个项目的值。
或者因为这是我第一次使用 Android。还有其他合适的方法来做到这一点吗?
谢谢
Hi I build a quiz application.
I have the following (values/)question.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="question">
<item name="correct">A</item>
<item name="wrong">B</item>
<item name="wrong">C</item>
<item name="wrong">D</item>
</string-array>
</resources>
I would like to have a question with four possible answers but when i retrieve my answers in Java.. I don't know which answer is correct. So I decided to use name attribute in the item tags to pass a value of 'correct' or 'wrong' answer.
Is there anyway to get the name along with the tag value?
because when i use String[] test = res.getStringArray(R.array.question); I can only get the value of each item in my array.
or because this is my 1st time in Android. is there other suitable approach to do this?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要使用Handler来解析xml。
要获取属性值,代码是:
尝试这些链接以供参考:
首先 和
第二个
You need to use Handler to parse the xml.
to get attribute value, code is :
Try these links for reference:
first and
second
我做了很多测验,一种很好的方法是将正确的选项始终放在问题之后的顶部。您可以使用随机函数在活动中显示时随机排列选项。
I have made many quizzes, and one nice way is to put correct option always on top, right after question. You can use random function to shuffle options while displaying in an activity.
在活动中:
String[] QuestionArray = getResources().getStringArray(R.id.question);
那么你有
in Activity:
String[] questionArray = getResources().getStringArray(R.id.question);
then you have