Android 如何通过其值而不是位置设置微调器默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10634180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:26:44  来源:igfitidea点击:

How to set Spinner Default by its Value instead of Position?

androidspinnersimplecursoradapterandroid-spinner

提问by vinothp

I have 1-50 records in the database. I am fetching those data using cursor and set those values to Spinner using Simple Cursor Adapter. Now what i need is i want to set one value say 39th value as default. But not by its position i want to set by its value.

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

I know how to set the spinner default by its position

我知道如何通过其位置设置微调器默认值

   spinner.setSelection(39) 

will set the spinner to that value.

将微调器设置为该值。

But i didn't have any idea about setting the spinner default by its value(text) in the database. I know the values in the database. For eg "books" is one of the value in the spinner. I need to set the spinner default as books.

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

Is there any possible way to do this?

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

采纳答案by vinothp

Finally, i solved the problem by using following way, in which the position of the spinner can be get by its string

最后,我通过使用以下方式解决了这个问题,其中可以通过其字符串获取微调器的位置

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();  
    } 

Calling the method

调用方法

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

I hope it will help for some one..

我希望它会帮助某人..

回答by Vinothkumar Arputharaj

If you are setting the spinner values by arraylistor arrayyou can set the spinner's selection by using the index of the value.

如果您正在设置微调器值,arraylist或者array您可以使用值的索引来设置微调器的选择。

String myString = "some value"; //the value you want the position for

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

int spinnerPosition = myAdap.getPosition(myString);

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

see the link How to set selected item of Spinner by value, not by position?

请参阅链接如何按值而不是按位置设置 Spinner 的选定项目?

回答by Svitlana

Compare string with value from index

将字符串与索引中的值进行比较

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;
             }
         }
     }

回答by MrG

this is how i did it:

我是这样做的:

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) {
            }
        });

回答by Rajat Jain

You can do it easily like this.

你可以像这样轻松地做到这一点。

String cls=student.getStudentClass();
class_spinner.setSelection(classArray.indexOf(cls),true);

回答by tùng ng?c ?ào

If the list you use for the spinner is an object then you can find its position like this

如果您用于微调器的列表是一个对象,那么您可以像这样找到它的位置

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;
}

using:

使用:

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