2

I make a spinner put the value in spinner using array,now even I was not select any value from spinner it automatically takes first value from spinner array, I want to set default value when nothing is selected form spinner array by user,means I get default value which I set ,when I was not selected any value, is this possible, and what is the use of onNothingSelected(AdapterView ....below code should run when user select manually any value from spinner but it run always and get first value which is in array,so please tell how to get default value when I nothing selected from spinner, can I use on nothing selected method..?

ArrayAdapter<String> CurrencyAdapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_dropdown_item_1line, Currency);
currency.setAdapter(CurrencyAdapter);
currency.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
    }
});
user3678528
  • 1,741
  • 2
  • 18
  • 24
shyam
  • 1,276
  • 4
  • 25
  • 51
  • Please take more care when you post code. Make sure the code is readable, you can improve this by removing extra empty lines, fixing indentation, and being consistent. – Lasse V. Karlsen Nov 04 '11 at 06:30

2 Answers2

0

Better set first value of array,the value what you want to set as default. And then you can check whether that value is selected or something else to figure out,is spinner selected or not??

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • actually i am setting first value in array is my current location which is changing dynamically and user also allowed to change the value from spinner array,as example my current location is USA and i set first value in spineer which i get from current laocation,and my second value in spinner also USA which is static so there will be repeating of same value in a spinner so is there any way to hide first value when i click on spinner,or is there any way not to run onItemSelected(AdapterView> parent, View view, int position, long id) without any selection made from spinner ? – shyam Nov 04 '11 at 06:02
  • It is not advisable to remove first item from spinner options when user click on spinner.Instead,you can highlight(make it 'bold') the selection when selection options are displayed,using custom adpater. That would work,i think. – Hiral Vadodaria Nov 04 '11 at 06:08
0

A related question I asked a couple of days ago could help you here, have a look at How to use Android Spinner like a drop-down list and see some of the answers which I found helpful.

And no, onNothingSelected() does not do what you're after... you want to have a look at the methods setSelection() and setSelectionInt() in the question I referred to.

Community
  • 1
  • 1
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72