android – How to set Spinner Default by its Value instead of Position?-ThrowExceptions

Exception or error:

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


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?

How to solve:

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

see the link How to set selected item of Spinner by value, not by 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;
    for(i=0;i< cursor.getCount()-1;i++)

        String locationVal = cursor.getString(cursor.getColumnIndex(RoadMoveDataBase.LT_LOCATION));  
            position = i+1;  
            position = 0;

Calling the method

    Spinner location2 = (Spinner)findViewById(;
    int location2id = getPostiton(cursor.getString(3),cursor);

I hope it will help for some one..


Compare string with value from index

private void selectSpinnerValue(Spinner spinner, String myString)
         int index = 0;
         for(int i = 0; i < spinner.getCount(); i++){


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

        // attaching data adapter to spinner
        spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
        spinner_age.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            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();
                    // show toast select gender
                    Toast.makeText(parent.getContext(), "none" + item, Toast.LENGTH_SHORT).show();
            public void onNothingSelected(AdapterView<?> parent) {


You can do it easily like this.

String cls=student.getStudentClass();


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++){
    return index;


 int index=selectSpinnerValue(ListOfSpinner,StringEquals);

Leave a Reply

Your email address will not be published. Required fields are marked *