0

I have a spinner and I'm getting its selected item position by MyOnItemSelectedListener. By the time I'm using an array adapter to load items to the spinner. I have loaded the items and it works perfectly. But I have a small problem. When I doesn't select a value then it shows position 0 but it has got the value of 1st array value. As shown in the below image,

enter image description here

What I want to do is when I haven't select a value then it should get any value from array. And when I select 1st item then only it should get 1st array value. Array is a created from json response so it is not possible to add a item to array manually.

I have used How to make an Android Spinner with initial text "Select One" (aaronvargas's answer) to add slect option to spinner as 1st selection.

How can I achieve this? Any help will be highly appreciated.

Community
  • 1
  • 1
modabeckham
  • 227
  • 5
  • 19
  • The first value in the array is at index position `0`. I'm note quite sure where your problem is ... – dhke Mar 18 '15 at 12:32

2 Answers2

0

You can use the logic like :

int selectedValue = -1;    
if(position<=0){
    //Means Item not selected
}else{
    selectedValue = array[position-1];
}
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
0

I think you can use something like I have shown.

 if(position==0){
        variable = 0;
    }else{
    //use ur logic here
    }
John David
  • 334
  • 3
  • 25