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

how to set default value in spinner in android studio
android spinner default value empty
android spinner set default selected item
how to set value in spinner dynamically in android
android spinner default text
set spinner position by value
set spinner position android
how to display selected value in spinner in android

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.

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?

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.

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?

android, 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  The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position.

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

Setting selected item of Spinner by value rather than by position , I want to preselect the value that is stored in the database for a Spinner but I can' .​.. .indexOf(value); getSpinnerField().setSelection(position); } Alternatively, you could override your spinner adapter, and provide an empty view for position 0 in your getView method, and a view with 0dp height in the getDropDownView method.

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

How to set selected item of Spinner by value instead of by position , Step 1 − Create a new project in Android Studio How to set selected item of Spinner by value instead of by position on Android? as an option and then check your mobile device which will display your default screen −. Then assigned the row number of the cursor to the spinner's selected position. It would be nice to have a similar function to pass in the id of the value you wish to select in the spinner when populating details on a form containing saved spinner results.

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

How to set selected item of Spinner by value, not by position?, getAdapter(); //cast to an ArrayAdapter int spinnerPosition = myAdap.getPosition(​myString); //set the default according to value mySpinner. si vous définissez les valeurs de spinner par arraylist ou array vous pouvez définir la sélection du spinner en utilisant l'index de la valeur.. 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

You can do it easily like this.

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

android.widget.Spinner.getItemAtPosition java code examples , How to set selected item of Spinner by value, not by position? How to set position in spinner? How to set Spinner Default by its Value instead of Postion? Set selected item of Spinner by value, not by position Code example extracted from Stack Overflow: 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

How to set selected item of Spinner by value, not by position , Assume your Spinner is named mSpinner, and it includes as one of its options: "​any value". To find and compare the position of "some value" in  Or the other way round, you can also set the customized default position of spinner,your way, using this snippet of code : ArrayAdapter<String> spinnerAdap = (ArrayAdapter<String>) mySpinner.getAdapter(); int spinnerPosition = messgAdap.getPosition(theDefaultValue); mySpinner.setSelection(spinnerPosition);

android How to set Spinner default value to null?, You need to take a couple of things into account, such as overriding getCount(), but parent, View view, int position, long id) { if(position>0) { label. that represents "no selection", and make it the initial selected item in the Spinner. Please consider using something else, like a ListView or GridView, instead of a Spinner. first of all if you have to set any data as default in spinner you have to create an Array or list because spinner.setselection method get input in only int. so At that time you have to pass that Array or list index to this method. got it ?

Spinners, Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the  How can i reset Spinner to default ? How can i set Default item selected programmatically after initializing adapter? spinner.setSelection(0); doesn't work

Comments
  • It seems as if all you need is to fetch a value from the db. Or am I misunderstanding something?
  • @keyser No.. I fetched value from the database and set it to spinner using simple cursor adapter.. Now the thing is i need to set one value of the spinner as default by its value not by its position.. have i explained clearly
  • No, it still seems that you just have to get the value from db itself and set it with pinner.setSelection(somepositionvalue).
  • possible duplicate of How to set selected item of Spinner by value, not by position?
  • Thanks for you answer.. I know its possible using ArrayAdapter.. Is the same possible by Simple Cursor Adapter??
  • @Vino Yes, checkout the following answer: stackoverflow.com/a/11667045/293280
  • This is the real answer.
  • Perhaps a bit leaner and more elegant version might look like this: 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; } }