[英][Android]How to get item number(position) from a String[]
I have a String[] that contains number of strings, what I'm trying to do is set a ProgressBar's progress according to which string is used. 我有一个包含字符串数的String [],我要做的是根据使用的字符串设置ProgressBar的进度。
For example, I have already determined number of strings and set max progress of progress bar accordingly; 例如,我已经确定了字符串的数量并相应地设置了进度条的最大进度; here is the list:
这是清单:
"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);
But now I need to set the progress of progressbar according to the item that is used, and I don't know how to get item position. 但是现在我需要根据使用的项目设置进度条的进度,我不知道如何获得项目位置。
So if I want to set a progress bar to the position of item five (in this case it would be 6) how can I do that - how can I get the position of item five? 因此,如果我想将进度条设置为第五项的位置(在这种情况下它将是6)我该怎么做 - 我如何获得第五项的位置?
essentially what you're looking for is a indexOf(...) ... 基本上你要找的是indexOf(...)......
since arrays don't have it, you'll have to search thru it to find the desired string. 因为数组没有它,你必须搜索它才能找到所需的字符串。 so something like this (feel free to optimize)
所以这样的事情(随意优化)
public int indexOfString(String searchString, String[] domain)
{
for(int i = 0; i < domain.length; i++)
if(searchString.equals(domain[i]))
return i;
return -1;
}
Then again, if you dynamically fetch your String[] data, it would be wiser to use an ArrayList and just invoke list.indexOf(myString);
然后,如果你动态获取String []数据,使用ArrayList并调用
list.indexOf(myString);
会更明智list.indexOf(myString);
You can use the Arrays
utility class: 您可以使用
Arrays
实用程序类:
List<String> list = Arrays.asList(new String[] {"First", "Second", "Third"});
int index = list.indexOf("Second"); // 1
cpu0freqslist[5]
这对你有帮助吗?
Ok let me break your query into parts... 好吧让我把你的查询分成几部分......
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.