英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论