如何从字符串数组中提取名称属性? [英] how to extract the name attribute from string array?

查看:44
本文介绍了如何从字符串数组中提取名称属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个测验应用程序.我有以下(值/)question.xml

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>

我想要一个有四种可能答案的问题,但是当我用 Java 检索答案时.. 我不知道哪个答案是正确的.所以我决定在项目标签中使用 name 属性来传递正确"或错误"答案的值.无论如何都可以获得名称和标签值吗?

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?

因为当我使用 String[] test = res.getStringArray(R.array.question);我只能获取数组中每个项目的值.

because when i use String[] test = res.getStringArray(R.array.question); I can only get the value of each item in my array.

或者因为这是我第一次使用 Android.还有其他合适的方法可以做到这一点吗?

or because this is my 1st time in Android. is there other suitable approach to do this?

谢谢

推荐答案

需要使用Handler来解析xml.获取属性值,代码为:

You need to use Handler to parse the xml. to get attribute value, code is :

attributes.getValue("name")

试试这些链接以供参考:首先

Try these links for reference: first and second

这篇关于如何从字符串数组中提取名称属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆