简体   繁体   English

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

[英][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.

相关问题 获取列表的项目位置和字符串(Android) - Get item position and string of a List (Android) 如何从Android中的“自定义ListView”获取被点击项的位置? - How to get clicked item position from Custom ListView in android? 如何获取两个字符串数组项的位置并存储在android中的每个单独的字符串数组中 - how to get two string array item position and store in each separate string array in android Android:如何获取ListView中单击的项目位置? - Android: how to get the clicked item position in the ListView? Android-如何获取ListView项目在绝对位置? - Android - How to get ListView item at absolute position? 如何从字符串中的特定位置获取数字? - How to fetch a number from specific position in a string? Android AutoCompleteTextView:获取项目位置 - Android AutoCompleteTextView: get item position Android如何在不使用onclicklistner的情况下从gridview获取所选项目的位置,而是使用ontouchlistner - Android How to get position of selected item from gridview without using onclicklistner, using ontouchlistner instead Android从AutoComplete中的Class结构中获取选定项的位置 - Android get selected item position from Class structure in AutoComplete Android Studio-单击列表视图中的项目,从该位置获取值 - Android Studio - Click item in listview, get a value from that position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM