我有一个字符串列表.xml
<resources>
<string name="app_name">XXXXXX MAPS</string>
<string-array name="Field">
<item>
AAAAA
</item>
<item>
BBBBB
</item>
<item>
CCCCC
</item>
<item>
DDDDD
</item>
<item>
EEEEE
</item>
<item>
FFFFF
</item>
<item>
GGGGG
</item>
我已将此列表放在列表视图中。当我单击列表视图时,我将获得字符串(例如,单击的字符串= EEEEE(
如何使用我知道已单击的字符串的信息从该字符串中找到 int 位置.xml?(例如,如果单击了字符串 EEEEE,我想让 Int 字符串单击= 5(
我想强调的是,我想以某种方式使用字符串中的字符串列表来获取它.xml。我不能使用类似的东西
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
获取 int 位置。
直接回答您的问题,如果您有字符串并希望在资源中获取数组中的位置:
String[] stringArray = getResources().getStringArray(R.array.Field);
获取资源数组中的位置:
Arrays.asList(stringArray).indexOf(string)
如果您有字符串列表,我可以在问题中看到的内容。您可以使用列表的 indexOf 方法来获取位置。例如:
List<String> strings = new ArrayList<>();
strings.add("aaa");
strings.add("bbb");
strings.add("ccc");
strings.add("ddd");
strings.add("eee");
int position = strings.indexOf("ccc");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
try {
//'position' refers to the 'int position' in the Override method
Log.e("This is your position", position.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});