如何根据值而不是位置设置Spinner的默认值?

66

我在数据库中有1-50条记录。我使用游标获取这些数据,并使用Simple Cursor Adapter将这些值设置到Spinner中。现在我需要的是将其中一个值,比如第39个值,设置为默认值。但是我不想按位置设置,我想按值设置。

我知道如何通过其位置设置Spinner的默认值。

   spinner.setSelection(39) 

将会把旋转器设为该值。

但是我不知道如何通过数据库中的值(文本)来设置旋转器的默认值。我知道数据库中的值。例如,“books”是旋转器中的一个值。我需要将旋转器的默认值设置为书籍。

有没有可能的方法可以做到这一点?


1
看起来你只需要从数据库中获取一个值。或者我有什么误解吗? - keyser
@keyser 不是这样的。我从数据库中获取了值,并使用简单游标适配器将其设置为微调控件。现在问题是,我需要根据其值而不是位置将微调控件的一个值设置为默认值。我解释清楚了吗? - vinothp
不,似乎你仍然需要从数据库中获取值,并使用pinner.setSelection(somepositionvalue)进行设置。 - Orest
1
可能是重复的问题:如何通过值而不是位置设置Spinner的选定项? - Joshua Pinter
7个回答

131
如果您正在使用 ArrayListArray 来设置 spinner 的值,您可以使用该值的索引来分配 spinner 的选择。
String myString = "some value"; //the value you want the position for

ArrayAdapter myAdapter = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter

int spinnerPosition = myAdapter.getPosition(myString);

//set the default according to the value
spinner.setSelection(spinnerPosition);

请查看链接 如何按值而不是位置设置Spinner的选定项?

此外,您可以直接使用方法来避免临时整数变量“spinnerPosition”:

getPosition(String item)

那么赋值代码将会是:

//set the default according to the value
spinner.setSelection(myAdapter.getPosition(myString));

1
谢谢你的回答。我知道可以使用ArrayAdapter实现。简单游标适配器能否实现相同功能? - vinothp
@Vino 是的,请查看以下答案:https://dev59.com/4XE95IYBdhLWcg3wQ7mb#11667045 - Joshua Pinter
3
这是真正的答案。 - Jared Burrows

15

最终,我通过以下方式解决了这个问题,在这种方式中,可以通过其字符串获取旋转器的位置

private int getPostiton(String locationid,Cursor cursor)
{
    int i;
    cursor.moveToFirst(); 
    for(i=0;i< cursor.getCount()-1;i++)
    {  

        String locationVal = cursor.getString(cursor.getColumnIndex(RoadMoveDataBase.LT_LOCATION));  
        if(locationVal.equals(locationid))
        { 
            position = i+1;  
            break;
        }
        else
        {
            position = 0;
        }
        cursor.moveToNext();  
    } 

调用该方法

    Spinner location2 = (Spinner)findViewById(R.id.spinner1);
    int location2id = getPostiton(cursor.getString(3),cursor);
    location2.setSelection(location2id);

我希望它能对某些人有所帮助..


也许更简洁、更优雅的版本看起来像这样:public class SpinnerHelper { public static int getPosition(String displayText, String fieldName, Cursor cursor) { int position = -1; cursor.moveToFirst(); while(cursor.moveToNext()) { String text = cursor.getString(cursor.getColumnIndex(fieldName)); if (text.equals(displayText)) { position = cursor.getPosition(); break; } } return position; }} - Alen Siljak

10

比较字符串与来自索引的值

private void selectSpinnerValue(Spinner spinner, String myString)
     {
         int index = 0;
         for(int i = 0; i < spinner.getCount(); i++){
             if(spinner.getItemAtPosition(i).toString().equals(myString)){
                 spinner.setSelection(i);
                 break;
             }
         }
     }

1
这是我做的方式:

String[] listAges = getResources().getStringArray(R.array.ages);

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listAges);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
        spinner_age.setAdapter(dataAdapter);
        spinner_age.setSelection(0);
        spinner_age.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();

                if(position > 0){
                    // get spinner value
                    Toast.makeText(parent.getContext(), "Age..." + item, Toast.LENGTH_SHORT).show();
                }else{
                    // show toast select gender
                    Toast.makeText(parent.getContext(), "none" + item, Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

1
你可以像这样轻松地完成它。
String cls=student.getStudentClass();
class_spinner.setSelection(classArray.indexOf(cls),true);

0
如果您在微调器中使用的列表是一个对象,那么您可以像这样找到它的位置。
private int selectSpinnerValue( List<Object> ListSpinner,String myString) 
{
    int index = 0;
    for(int i = 0; i < ListSpinner.size(); i++){
      if(ListSpinner.get(i).getValueEquals().equals(myString)){
          index=i;
          break;
      }
    }
    return index;
}

使用:

 int index=selectSpinnerValue(ListOfSpinner,StringEquals);
    spinner.setSelection(index,true);

-1

最佳的 Kotlin 方式:

spinner.setSelection(0)

整数是你想要的位置


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接