Android Spinner: how to set default value

While using a Spinner Widget in Android, we face a peculiar problem. Lets say that I have a spinner for choosing the City from a list of cities. This is how I want this to look like. However, here likes the problem.

                         

The code that I wrote is as follows

String[] city_list = new String[3];
city_list[0] = "Delhi";
city_list[1] = "Gurgaon";
city_list[2] = "Noida";

ArrayAdapter<String > aa=new ArrayAdapter<String> (getApplicationContext(),         
R.layout.spinner_item, city_list);

city_spinner = (Spinner) findViewById(R.id.spinner1);
city_spinner.setAdapter(aa);

By default Delhi, being the first entry in the ArrayList aa which is the adapter for the spinner, it is shown by the Spinner by default. This is where the problem starts. Since the first entry is shown, the city_spinner.setOnItemSelectedListener identifies this as a selected item. Hence it does not wait for the user to select anything, it simply assumes that Delhi is the choice from user and moves ahead in code. To bypass this, I tried to keep a counter to identify the first time SetOnItemSelectedListener is called, and handle it to do nothing. My code looks like this.

city_spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
      if (spinner_count == 0)
      {
         spinner_count++;
      }
      else
      {
         //do what you want to do with the selected field.
      }
    }
});

The spinner_count will be 0 when the first time the activity is rendered, hence as soon as it displays first item (Delhi in this case) by default, it will trigger onItemSelected, but since we have handled it, nothing will happen and the UI will wait for the user to select an Item.

This created another challenge. So when I select Gurgaon or Noida with are the 2nd and 3rd entries, it is working fine. However, if I select Delhi from the dropdown, nothing happens. This is because, since the previous Item Selected by default was Delhi, and I am again trying to select Delhi, it is not getting identified as a new Item selection, hence this portion of the code is not getting called at all. So net net, you cannot select the first entry using this approach. 

So I tried a new approach. I added an item "Choose City" in city_list as the first element. Then inside onItemSelected, i specifically handed the case where if 'Choose City' is selected, then do nothing. 

The code is as follows.

String[] city_list = new String[4];

city_list[0] = "Choose City";
city_list[1] = "Delhi";
city_list[2] = "Gurgaon";
city_list[3] = "Noida";

ArrayAdapter<String > aa=new ArrayAdapter<String> (getApplicationContext(), 
R.layout.spinner_item, city_list);


city_spinner = (Spinner) findViewById(R.id.spinner1);
city_spinner.setAdapter(aa);

city_spinner.setOnItemSelectedListener(new OnItemSelectedListener()

{
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
     {
         if (parent.getItemAtPosition(position).equals("Choose City"))
         {
             //do nothing.
         }
         else
         {
            // write code on what you want to do with the item selection
         }
     }
});
The output will be as follows as works as expected.


No comments:

Post a Comment