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

6

66

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?

Mulderig answered 17/5, 2012 at 10:40 Comment(4)
It seems as if all you need is to fetch a value from the db. Or am I misunderstanding something?Va
@Va 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 clearlyMulderig
No, it still seems that you just have to get the value from db itself and set it with pinner.setSelection(somepositionvalue).Trenttrento
possible duplicate of How to set selected item of Spinner by value, not by position?Vasquez
M
15

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

Mulderig answered 11/6, 2012 at 14:30 Comment(1)
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; } }Cutlor
B
131

If you are setting the spinner values by ArrayList or Array you can assign the spinner's selection by using the value's index.

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

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

Also, you can avoid the temporary integer variable "spinnerPosition " by directly using the method:

getPosition(String item)

Then the assigning code will be:

//set the default according to the value
spinner.setSelection(myAdapter.getPosition(myString));
Brookhouse answered 17/5, 2012 at 11:43 Comment(3)
Thanks for you answer.. I know its possible using ArrayAdapter.. Is the same possible by Simple Cursor Adapter??Mulderig
@Vino Yes, checkout the following answer: https://mcmap.net/q/93159/-how-to-set-selected-item-of-spinner-by-value-not-by-positionVasquez
This is the real answer.Beaut
M
15

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

Mulderig answered 11/6, 2012 at 14:30 Comment(1)
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; } }Cutlor
M
10

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;
             }
         }
     }
Meretricious answered 21/4, 2016 at 9:44 Comment(0)
T
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) {
            }
        });
Trussell answered 28/6, 2016 at 8:19 Comment(0)
C
1

You can do it easily like this.

String cls=student.getStudentClass();
class_spinner.setSelection(classArray.indexOf(cls),true);
Cowey answered 25/5, 2018 at 4:41 Comment(0)
A
0

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);
Anissaanita answered 9/1, 2020 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.