329

我有一个更新视图,我需要在其中为 Spinner 预选存储在数据库中的值。

我正在考虑这样的事情,但是Adapter没有indexOf方法,所以我被卡住了。

void setSpinner(String value)
{
    int pos = getSpinnerField().getAdapter().indexOf(value);
    getSpinnerField().setSelection(pos);
}
4

25 回答 25

690

假设你Spinner的名字是mSpinner,并且它包含作为它的选择之一:“一些值”。

要在 Spinner 中查找和比较“某个值”的位置,请使用以下命令:

String compareValue = "some value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (compareValue != null) {
    int spinnerPosition = adapter.getPosition(compareValue);
    mSpinner.setSelection(spinnerPosition);
}
于 2010-11-19T18:18:57.560 回答
155

根据值设置微调器的一种简单方法是

mySpinner.setSelection(getIndex(mySpinner, myValue));

 //private method of your class
 private int getIndex(Spinner spinner, String myString){
     for (int i=0;i<spinner.getCount();i++){
         if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(myString)){
             return i;
         }
     }

     return 0;
 } 

复杂代码的方式已经存在,这更简单。

于 2013-02-01T05:56:29.933 回答
37

根据Merrill 的回答,我想出了这个单行解决方案......它不是很漂亮,但你可以责怪维护代码的Spinner人忽略了包含为此执行此操作的函数。

mySpinner.setSelection(((ArrayAdapter<String>)mySpinner.getAdapter()).getPosition(myString));

您将收到有关如何ArrayAdapter<String>取消选中 a 的演员表的警告...实际上,您可以ArrayAdapter像 Merrill 那样使用 a ,但这只是将一个警告换成另一个警告。

如果警告导致问题,只需添加

@SuppressWarnings("unchecked")

到方法签名或声明之上。

于 2012-08-22T20:41:17.440 回答
35

我为我的 Spinners 中的所有项目保留了一个单独的 ArrayList。这样我可以在 ArrayList 上执行 indexOf,然后使用该值在 Spinner 中设置选择。

于 2010-03-05T21:44:35.563 回答
13

如果您使用的是字符串数组,这是最好的方法:

int selectionPosition= adapter.getPosition("YOUR_VALUE");
spinner.setSelection(selectionPosition);
于 2015-03-18T18:22:23.230 回答
11

使用以下行选择使用值:

mSpinner.setSelection(yourList.indexOf("value"));
于 2016-12-18T19:28:25.920 回答
10

你也可以用这个,

String[] baths = getResources().getStringArray(R.array.array_baths);
mSpnBaths.setSelection(Arrays.asList(baths).indexOf(value_here));
于 2013-10-17T10:44:44.627 回答
8

如果您需要在任何旧适配器上有一个 indexOf 方法(并且您不知道底层实现),那么您可以使用它:

private int indexOf(final Adapter adapter, Object value)
{
    for (int index = 0, count = adapter.getCount(); index < count; ++index)
    {
        if (adapter.getItem(index).equals(value))
        {
            return index;
        }
    }
    return -1;
}
于 2012-08-29T22:26:45.710 回答
7

根据 Merrill 的回答,这里是如何使用 CursorAdapter

CursorAdapter myAdapter = (CursorAdapter) spinner_listino.getAdapter(); //cast
    for(int i = 0; i < myAdapter.getCount(); i++)
    {
        if (myAdapter.getItemId(i) == ordine.getListino() )
        {
            this.spinner_listino.setSelection(i);
            break;
        }
    }
于 2012-07-26T10:07:56.800 回答
4

你可以使用这种方式,只是让你的代码更简单,更清晰。

ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinnerCountry.getAdapter();
int position = adapter.getPosition(obj.getCountry());
spinnerCountry.setSelection(position);

希望能帮助到你。

于 2019-07-24T09:24:39.483 回答
3

这是我的解决方案

List<Country> list = CountryBO.GetCountries(0);
CountriesAdapter dataAdapter = new CountriesAdapter(this,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnCountries.setAdapter(dataAdapter);
spnCountries.setSelection(dataAdapter.getItemIndexById(userProfile.GetCountryId()));

和下面的 getItemIndexById

public int getItemIndexById(String id) {
    for (Country item : this.items) {
        if(item.GetId().toString().equals(id.toString())){
            return this.items.indexOf(item);
        }
    }
    return 0;
}

希望这有帮助!

于 2012-07-05T04:21:56.827 回答
3

我正在使用自定义适配器,因为这段代码就足够了:

yourSpinner.setSelection(arrayAdapter.getPosition("Your Desired Text"));

因此,您的代码片段将如下所示:

void setSpinner(String value)
    {
         yourSpinner.setSelection(arrayAdapter.getPosition(value));
    }
于 2015-06-10T09:03:01.687 回答
3

这是我通过字符串获取索引的简单方法。

private int getIndexByString(Spinner spinner, String string) {
    int index = 0;

    for (int i = 0; i < spinner.getCount(); i++) {
        if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(string)) {
            index = i;
            break;
        }
    }
    return index;
}
于 2016-10-19T03:15:37.810 回答
2

如果您使用的是 a SimpleCursorAdaptercolumnName用于填充的 db 列的名称在哪里),请执行以下操作spinner

private int getIndex(Spinner spinner, String columnName, String searchString) {

    //Log.d(LOG_TAG, "getIndex(" + searchString + ")");

    if (searchString == null || spinner.getCount() == 0) {

        return -1; // Not found
    }
    else {

        Cursor cursor = (Cursor)spinner.getItemAtPosition(0);

        int initialCursorPos = cursor.getPosition(); //  Remember for later

        int index = -1; // Not found
        for (int i = 0; i < spinner.getCount(); i++) {

            cursor.moveToPosition(i);
            String itemText = cursor.getString(cursor.getColumnIndex(columnName));

            if (itemText.equals(searchString)) {
                index = i; // Found!
                break;
            }
        }

        cursor.moveToPosition(initialCursorPos); // Leave cursor as we found it.

        return index;
    }
}

此外(Akhil 的答案的改进)如果您从数组中填充 Spinner,这是相应的方法:

private int getIndex(Spinner spinner, String searchString) {

    if (searchString == null || spinner.getCount() == 0) {

        return -1; // Not found

    }
    else {

        for (int i = 0; i < spinner.getCount(); i++) {
            if (spinner.getItemAtPosition(i).toString().equals(searchString)) {
                return i; // Found!
            }
        }

        return -1; // Not found
    }
};
于 2015-09-03T14:05:47.303 回答
1

假设您需要从资源的字符串数组中填充微调器,并且您希望保留从服务器中选择的值。因此,这是在微调器中设置从服务器中选择的值的一种方法。

pincodeSpinner.setSelection(resources.getStringArray(R.array.pincodes).indexOf(javaObject.pincode))

希望能帮助到你!PS 代码在 Kotlin 中!

于 2019-01-10T11:44:48.327 回答
1

如果您将 XML 数组设置为 XML 布局中的微调器,则可以执行此操作

final Spinner hr = v.findViewById(R.id.chr);
final String[] hrs = getResources().getStringArray(R.array.hours);
if(myvalue!=null){
   for (int x = 0;x< hrs.length;x++){
      if(myvalue.equals(hrs[x])){
         hr.setSelection(x);
      }
   }
}
于 2019-01-27T20:09:42.007 回答
0

实际上有一种方法可以使用 AdapterArray 上的索引搜索来获得它,所有这些都可以通过反射来完成。我什至更进一步,因为我有 10 个 Spinner,并且想从我的数据库中动态设置它们,并且数据库只保存值而不是文本,因为 Spinner 实际上每周都在变化,所以该值是我在数据库中的 ID 号。

 // Get the JSON object from db that was saved, 10 spinner values already selected by user
 JSONObject json = new JSONObject(string);
 JSONArray jsonArray = json.getJSONArray("answer");

 // get the current class that Spinner is called in 
 Class<? extends MyActivity> cls = this.getClass();

 // loop through all 10 spinners and set the values with reflection             
 for (int j=1; j< 11; j++) {
      JSONObject obj = jsonArray.getJSONObject(j-1);
      String movieid = obj.getString("id");

      // spinners variable names are s1,s2,s3...
      Field field = cls.getDeclaredField("s"+ j);

      // find the actual position of value in the list     
      int datapos = indexedExactSearch(Arrays.asList(Arrays.asList(this.data).toArray()), "value", movieid) ;
      // find the position in the array adapter
      int pos = this.adapter.getPosition(this.data[datapos]);

      // the position in the array adapter
      ((Spinner)field.get(this)).setSelection(pos);

}

只要字段位于对象的顶层,您几乎可以在任何列表上使用以下索引搜索。

    /**
 * Searches for exact match of the specified class field (key) value within the specified list.
 * This uses a sequential search through each object in the list until a match is found or end
 * of the list reached.  It may be necessary to convert a list of specific objects into generics,
 * ie: LinkedList&ltDevice&gt needs to be passed as a List&ltObject&gt or Object[&nbsp] by using 
 * Arrays.asList(device.toArray(&nbsp)).
 * 
 * @param list - list of objects to search through
 * @param key - the class field containing the value
 * @param value - the value to search for
 * @return index of the list object with an exact match (-1 if not found)
 */
public static <T> int indexedExactSearch(List<Object> list, String key, String value) {
    int low = 0;
    int high = list.size()-1;
    int index = low;
    String val = "";

    while (index <= high) {
        try {
            //Field[] c = list.get(index).getClass().getDeclaredFields();
            val = cast(list.get(index).getClass().getDeclaredField(key).get(list.get(index)) , "NONE");
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (val.equalsIgnoreCase(value))
            return index; // key found

        index = index + 1;
    }

    return -(low + 1);  // key not found return -1
}

可以在这里为所有原语创建的 Cast 方法是用于 string 和 int 的一种。

        /**
 *  Base String cast, return the value or default
 * @param object - generic Object
 * @param defaultValue - default value to give if Object is null
 * @return - returns type String
 */
public static String cast(Object object, String defaultValue) {
    return (object!=null) ? object.toString() : defaultValue;
}


    /**
 *  Base integer cast, return the value or default
 * @param object - generic Object
 * @param defaultValue - default value to give if Object is null
 * @return - returns type integer
 */
public static int cast(Object object, int defaultValue) { 
    return castImpl(object, defaultValue).intValue();
}

    /**
 *  Base cast, return either the value or the default
 * @param object - generic Object
 * @param defaultValue - default value to give if Object is null
 * @return - returns type Object
 */
public static Object castImpl(Object object, Object defaultValue) {
    return object!=null ? object : defaultValue;
}
于 2011-05-13T02:42:35.543 回答
0

尝试在使用 cursorLoader 填充的微调器中选择正确的项目时,我遇到了同样的问题。我从表 1 中检索了我想首先选择的项目的 ID,然后使用 CursorLoader 填充微调器。在 onLoadFinished 中,我循环遍历填充微调器适配器的光标,直到找到与我已有的 id 匹配的项目。然后将光标的行号分配给微调器的选定位置。在包含保存的微调器结果的表单上填充详细信息时,如果有一个类似的函数来传递您希望在微调器中选择的值的 id,那就太好了。

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {  
  adapter.swapCursor(cursor);

  cursor.moveToFirst();

 int row_count = 0;

 int spinner_row = 0;

  while (spinner_row < 0 || row_count < cursor.getCount()){ // loop until end of cursor or the 
                                                             // ID is found 

    int cursorItemID = bCursor.getInt(cursor.getColumnIndexOrThrow(someTable.COLUMN_ID));

    if (knownID==cursorItemID){
    spinner_row  = row_count;  //set the spinner row value to the same value as the cursor row 

    }
cursor.moveToNext();

row_count++;

  }

}

spinner.setSelection(spinner_row ); //set the selected item in the spinner

}
于 2013-01-11T03:38:29.570 回答
0

要使应用程序记住最后选择的微调器值,您可以使用以下代码:

  1. 下面的代码读取微调器值并相应地设置微调器位置。

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    int spinnerPosition;
    
    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this, R.array.ccy_array,
            android.R.layout.simple_spinner_dropdown_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_list_item_activated_1);
    // Apply the adapter to the spinner
    spinner1.setAdapter(adapter1);
    // changes to remember last spinner position
    spinnerPosition = 0;
    String strpos1 = prfs.getString("SPINNER1_VALUE", "");
    if (strpos1 != null || !strpos1.equals(null) || !strpos1.equals("")) {
        strpos1 = prfs.getString("SPINNER1_VALUE", "");
        spinnerPosition = adapter1.getPosition(strpos1);
        spinner1.setSelection(spinnerPosition);
        spinnerPosition = 0;
    }
    
  2. 并将下面的代码放在您知道最新微调器值存在的地方,或者根据需要放在其他地方。这段代码基本上是在 SharedPreferences 中写入 spinner 的值。

        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        String spinlong1 = spinner1.getSelectedItem().toString();
        SharedPreferences prfs = getSharedPreferences("WHATEVER",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prfs.edit();
        editor.putString("SPINNER1_VALUE", spinlong1);
        editor.commit();
    
于 2014-07-31T13:00:51.143 回答
0

由于之前的一些答案是非常正确的,我只想确保你们没有人陷入这样的问题。

如果将值设置为ArrayListusing String.format,则必须使用相同的字符串结构获取值的位置String.format

一个例子:

ArrayList<String> myList = new ArrayList<>();
myList.add(String.format(Locale.getDefault() ,"%d", 30));
myList.add(String.format(Locale.getDefault(), "%d", 50));
myList.add(String.format(Locale.getDefault(), "%d", 70));
myList.add(String.format(Locale.getDefault(), "%d", 100));

您必须像这样获得所需价值的位置:

myList.setSelection(myAdapter.getPosition(String.format(Locale.getDefault(), "%d", 70)));

否则,您将得到-1, item not found!

我用阿拉伯语Locale.getDefault()是因为。

我希望这对你有帮助。

于 2016-05-16T15:38:10.947 回答
0

这是我希望的完整解决方案。我有以下枚举:

public enum HTTPMethod {GET, HEAD}

用于以下课程

public class WebAddressRecord {
...
public HTTPMethod AccessMethod = HTTPMethod.HEAD;
...

通过 HTTPMethod 枚举成员设置微调器的代码:

    Spinner mySpinner = (Spinner) findViewById(R.id.spinnerHttpmethod);
    ArrayAdapter<HTTPMethod> adapter = new ArrayAdapter<HTTPMethod>(this, android.R.layout.simple_spinner_item, HTTPMethod.values());
    mySpinner.setAdapter(adapter);
    int selectionPosition= adapter.getPosition(webAddressRecord.AccessMethod);
    mySpinner.setSelection(selectionPosition);

whereR.id.spinnerHttpmethod在布局文件中定义,android.R.layout.simple_spinner_item由 android-studio 提供。

于 2016-12-26T13:29:57.810 回答
0
YourAdapter yourAdapter =
            new YourAdapter (getActivity(),
                    R.layout.list_view_item,arrData);

    yourAdapter .setDropDownViewResource(R.layout.list_view_item);
    mySpinner.setAdapter(yourAdapter );


    String strCompare = "Indonesia";

    for (int i = 0; i < arrData.length ; i++){
        if(arrData[i].getCode().equalsIgnoreCase(strCompare)){
                int spinnerPosition = yourAdapter.getPosition(arrData[i]);
                mySpinner.setSelection(spinnerPosition);
        }
    }
于 2018-02-12T03:19:55.747 回答
0

非常简单,只需使用getSelectedItem();

例如:

ArrayAdapter<CharSequence> type=ArrayAdapter.createFromResource(this,R.array.admin_typee,android.R.layout.simple_spinner_dropdown_item);
        type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mainType.setAdapter(type);

String group=mainType.getSelectedItem().toString();

上述方法返回一个字符串值

在上面R.array.admin_type是值中的字符串资源文件

只需在 values>>strings 中创建一个 .xml 文件

于 2018-06-28T16:40:37.210 回答
0

因为我需要一些也适用于Localization的东西,所以我想出了这两种方法:

    private int getArrayPositionForValue(final int arrayResId, final String value) {
        final Resources english = Utils.getLocalizedResources(this, new Locale("en"));
        final List<String> arrayValues = Arrays.asList(english.getStringArray(arrayResId));

        for (int position = 0; position < arrayValues.size(); position++) {
            if (arrayValues.get(position).equalsIgnoreCase(value)) {
                return position;
            }
        }
        Log.w(TAG, "getArrayPosition() --> return 0 (fallback); No index found for value = " + value);
        return 0;
    }

如您所见,我还偶然发现了arrays.xml 和我正在比较的文件之间区分大小写的额外复杂性。value如果你没有这个,上面的方法可以简化为:

return arrayValues.indexOf(value);

静态辅助方法

public static Resources getLocalizedResources(Context context, Locale desiredLocale) {
        Configuration conf = context.getResources().getConfiguration();
        conf = new Configuration(conf);
        conf.setLocale(desiredLocale);
        Context localizedContext = context.createConfigurationContext(conf);
        return localizedContext.getResources();
    }
于 2019-04-26T13:23:19.597 回答
-3

你必须通过像 REPEAT[position] 这样的位置来传递你的自定义适配器。它工作正常。

于 2015-03-18T02:27:24.147 回答