如何获取字符串数组的索引

How to get the index of string-array

strings.xml 中,我有 string-array 填充 spinner。 我有一个笔记应用程序,用户可以在其中 select 来自微调器的主题并在回收站视图中显示。 因此,在用户添加注释后,如果用户单击该注释,则它应该重定向到该微调器并自动 select 之前 select 编辑的主题。

我试图将位置设置为 sub_spinner.setSelection(subPosition) 但我不知道如何获得 subPosition

String.xml

<string-array name="subject">
    <item>Communication English</item>
    <item>Computer Graphics</item>
    <item>COA</item>
    <item>Data Communication</item>
    <item>Instrumentation II</item>
    <item>Software Engineerinng</item>
</string-array>

所以如果我有 sub 作为 COA 那么我应该得到 2 的索引。

因为您在 xml 中有主题 string-array。您可以使用

在 java 代码中获取字符串数组
List<String> list= Arrays.asList(getResources().getStringArray(R.array.subject));

然后为了得到所选主题的索引,你可以在SpinnerListView上实现一个OnItemSelectedListener;您填充字符串数组的任何内容。

OnItemSelectedListener 实现有一个类似 onItemSelected 的带参数的回调,您需要 position 来提供所选项目的位置。然后你可以这样做:

setSelection(position)

OnItemSelectedListener 里面。如果您需要关于如何实施的进一步指导,您可以 post 一些更多的代码,我将为您提供执行此操作的确切代码。