如何将Spinner默认设置为其值而不是位置?

时间:2022-07-05 00:20:25

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条记录。我使用游标来获取这些数据,并使用简单的光标适配器将这些值设置为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.

但是我不知道如何在数据库中设置spinner默认值(文本)。我知道数据库中的值。因为“书”是旋转器的价值之一。我需要将spinner默认设置为books。

Is there any possible way to do this?

有可能的办法吗?

5 个解决方案

#1


98  

If you are setting the spinner values by arraylist or array you can set the spinner's selection by using the index of the value.

如果您正在通过arraylist或数组设置微调控制项的值,您可以通过使用该值的索引来设置微调控制项的选择。

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?

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

#2


14  

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

最后,我用下面的方法解决了这个问题,在这个问题中,spinner的位置可以通过它的字符串得到。

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..

我希望这对谁都有帮助。

#3


9  

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

#4


1  

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

#5


0  

You can do it easily like this.

这样做很容易。

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

#1


98  

If you are setting the spinner values by arraylist or array you can set the spinner's selection by using the index of the value.

如果您正在通过arraylist或数组设置微调控制项的值,您可以通过使用该值的索引来设置微调控制项的选择。

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?

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

#2


14  

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

最后,我用下面的方法解决了这个问题,在这个问题中,spinner的位置可以通过它的字符串得到。

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..

我希望这对谁都有帮助。

#3


9  

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

#4


1  

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

#5


0  

You can do it easily like this.

这样做很容易。

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