Pooja Patwa
Pooja Patwa

Reputation: 9

How to set a particular position as default in spinner?

I am getting list of data from server and setting in spinner through setAdapter, but what data is coming on 3rd position I want to set that as default(0th position). Ex. {Mango, Banana, apple} ; in spinner apple should be default instead of Mango

         else if 
             (mListener.getSelection().get(0).
              getGenLovs().get(i).getLovId().
              equalsIgnoreCase(File_Key.AB_CUST_TITLE)) 
             {
                        binding.spinTitle.setAdapter(new 
                        GenLovsSpinner(getContext(), 
                        mListener.getSelection().get(0). 
                        getGenLovs().get(i).getValDes()));
                    }

I have tried this

             String cls= 
             String.valueOf(mListener.getSelection().
             get(0).getGenLovs().get(i).getValDes().get(3));
             binding.spinTitle.setSelection(Integer.parseInt(cls),true);

Here when I am using above code I am getting NumberFormatException

Upvotes: 1

Views: 67

Answers (4)

MurugananthamS
MurugananthamS

Reputation: 2405

try this
Use the following: spinnerObject.setSelection(position).

Upvotes: 0

Govind Prajapati
Govind Prajapati

Reputation: 206

See you are setting up any list or array to spinner adapter. If you want to set particular as default then try this for example :- Let you are setting dataList to spinner adapter after setAdapter() for selection

either spinner.setSelection(dataList.indexOf("apple"),true) or spinner.setSelection(2,true) as your third data has index 2

Upvotes: 1

Athira
Athira

Reputation: 1213

Just give binding.spinTitle.setSelection(2);

Upvotes: 0

Sandeep Malik
Sandeep Malik

Reputation: 1976

binding.spinTitle.setSelection(Integer.parseInt(cls),true);

use this insted of above line

binding.spinTitle.setSelection(Integer.valueOf(cls));

Upvotes: 1

Related Questions