如何获取XML字符串资源的键和值

huangapple go评论40阅读模式
英文:

How to get the keys and values of an xml string resource

问题

"and this" 如何获取该值

英文:
<string id="I want To Get This">and this</string>
<string id="I want To Get This">and this</string>
<string id="I want To Get This">and this</string>

How to get the value

答案1

得分: 1

To get the value from String Resource you use

从字符串资源中获取值,您可以使用:

String stringToCompare= getString(R.string.my_custom_id);

String stringToCompare = getString(R.string.my_custom_id);

After that you can add that value to a list

之后,您可以将该值添加到列表中:

然后,您可以将该值添加到列表中:

myList.add(value)

myList.add(value)

Now if you say all similar I suppose you mean equal values or at least same characters. For that you could iterate over all the string values via reflection

现在,如果您说所有相似的,我想您指的是相等的值,或者至少是相同的字符。为此,您可以通过反射遍历所有字符串值:

现在,如果你说所有相似的,我想你指的是相等的值,或者至少是相同的字符。为此,你可以通过反射遍历所有字符串值:

for (Field field : R.string.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class)) {
try {
if (field.getName().toLoweCase().equals(stringToCompare.toLowerCase())) {
int id = field.getInt(null);
myList.add(getString(id))
}
} catch (IllegalArgumentException e) {
// ignore
} catch (IllegalAccessException e) {
// ignore
}
}
}

for (Field field : R.string.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class)) {
try {
if (field.getName().toLoweCase().equals(stringToCompare.toLowerCase())) {
int id = field.getInt(null);
myList.add(getString(id))
}
} catch (IllegalArgumentException e) {
// ignore
} catch (IllegalAccessException e) {
// ignore
}
}
}

Or can find another way of doing that iteration here https://stackoverflow.com/questions/10633456/how-to-easily-iterate-over-all-strings-within-the-strings-xml-resource-file

或者您可以在此处找到另一种执行该迭代的方法 https://stackoverflow.com/questions/10633456/how-to-easily-iterate-over-all-strings-within-the-strings-xml-resource-file

英文:

To get the value from String Resource you use

String stringToCompare= getString(R.string.my_custom_id);

After that you can add that value to a list

myList.add(value)

Now if you say all similar I suppose you mean equal values or at least same characters. For that you could iterate over all the string values via reflection

for (Field field : R.string.class.getDeclaredFields()) {
 if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class)) {
   try {
     if (field.getName().toLoweCase().equals(stringToCompare.toLowerCase())) {
       int id = field.getInt(null);
       myList.add(getString(id))
     }
   } catch (IllegalArgumentException e) {
      // ignore
   } catch (IllegalAccessException e) {
     // ignore
    }
  }
}

Or can find another way of doing that iteration here https://stackoverflow.com/questions/10633456/how-to-easily-iterate-over-all-strings-within-the-strings-xml-resource-file

huangapple
  • 本文由 发表于 2020年7月28日 15:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定