[Android]如何从String []获取项目编号(位置)

ped*_*dja 3 java android android-layout

我有一个包含字符串数的String [],我要做的是根据使用的字符串设置ProgressBar的进度.

例如,我已经确定了字符串的数量并相应地设置了进度条的最大进度; 这是清单:

 "zero one two three four five six seven eight nine...."
Run Code Online (Sandbox Code Playgroud)

..

 String[] cpu0freqslist = cpu0freqs.split("\\s");
 countcpu0 = cpu0freqslist.length;
Run Code Online (Sandbox Code Playgroud)

..

 ProgressBar cpu0progbar = (ProgressBar)findViewById(R.id.progressBar1);
 cpu0progbar.setMax(countcpu0);
Run Code Online (Sandbox Code Playgroud)

但是现在我需要根据使用的项目设置进度条的进度,我不知道如何获得项目位置.

因此,如果我想将进度条设置为第五项的位置(在这种情况下它将是6)我该怎么做 - 我如何获得第五项的位置?

Sha*_*ark 6

基本上你要找的是indexOf(...)......

因为数组没有它,你必须搜索它才能找到所需的字符串.所以这样的事情(随意优化)

 public int indexOfString(String searchString, String[] domain)
 {
     for(int i = 0; i < domain.length; i++)
        if(searchString.equals(domain[i]))
           return i;

     return -1;
 }
Run Code Online (Sandbox Code Playgroud)

然后,如果您动态获取String []数据,使用ArrayList并调用它会更明智 list.indexOf(myString);


Ada*_*nos 6

您可以使用Arrays实用程序类:

List<String> list = Arrays.asList(new String[] {"First", "Second", "Third"});
int index = list.indexOf("Second"); // 1
Run Code Online (Sandbox Code Playgroud)