[英][Android]How to get item number(position) from a String[]
我有一个包含字符串数的String [],我要做的是根据使用的字符串设置ProgressBar的进度。
例如,我已经确定了字符串的数量并相应地设置了进度条的最大进度; 这是清单:
"zero one two three four five six seven eight nine...."
..
String[] cpu0freqslist = cpu0freqs.split("\\s");
countcpu0 = cpu0freqslist.length;
..
ProgressBar cpu0progbar = (ProgressBar)findViewById(R.id.progressBar1);
cpu0progbar.setMax(countcpu0);
但是现在我需要根据使用的项目设置进度条的进度,我不知道如何获得项目位置。
因此,如果我想将进度条设置为第五项的位置(在这种情况下它将是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;
}
然后,如果你动态获取String []数据,使用ArrayList并调用list.indexOf(myString);
会更明智list.indexOf(myString);
您可以使用Arrays
实用程序类:
List<String> list = Arrays.asList(new String[] {"First", "Second", "Third"});
int index = list.indexOf("Second"); // 1
cpu0freqslist[5]
这对你有帮助吗?
好吧让我把你的查询分成几部分......
String[] arr = {"First","Second","Third"}; // String array with 3 elements
for(int i=0 ; i<arr.length ; i++){
int j = i ; // Position of the element
String s = a[i] ; // Element Itself
System.out.println("The "+i+" element is "+s);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.