Toast msg displaying 2nd item when selected the 1st one for spinner
<p>NothingSelectedSpinnerAdapter.java</p>

<pre><code>package com.example.wastewarriorsnew;


public class NothingSelectedSpinnerAdapter extends Activity implements SpinnerAdapter, ListAdapter {
    protected static final int EXTRA = 1;
    protected SpinnerAdapter adapter;
    protected Context context;
    protected int nothingSelectedLayout;
    protected int nothingSelectedDropdownLayout;
    protected LayoutInflater layoutInflater;


    public NothingSelectedSpinnerAdapter(
              SpinnerAdapter spinnerAdapter,
              int nothingSelectedLayout, Context context) {

                this(spinnerAdapter, nothingSelectedLayout, -1, context);
            }


            public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
                    int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
                this.adapter = spinnerAdapter;
                this.context = context;
                this.nothingSelectedLayout = nothingSelectedLayout;
                this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
                layoutInflater = LayoutInflater.from(context);
            }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nothing_selected_spinner_adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater()
                .inflate(R.menu.nothing_selected_spinner_adapter, menu);
        return true;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
         int count = adapter.getCount();
            return count == 0 ? 0 : count + EXTRA;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position == 0 ? null : adapter.getItem(position - EXTRA);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
         return position &gt;= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // This provides the View for the Selected Item in the Spinner, not
        // the dropdown (unless dropdownView is not set).
        if (position == 0) {
            return getNothingSelectedView(parent);
        }
        return adapter.getView(position - EXTRA, null, parent); // Could re-use
                                                 // the convertView if possible.

    }

    protected View getNothingSelectedView(ViewGroup parent) {
        return layoutInflater.inflate(nothingSelectedLayout, parent, false);
    }
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return adapter.hasStableIds();
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
         return adapter.isEmpty();
         }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub
        adapter.registerDataSetObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub
        adapter.unregisterDataSetObserver(observer);
    }

    @Override
    public boolean areAllItemsEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return position != 0; // Don't allow the 'nothing selected'
        // item to be picked.
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         // Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
        // Spinner does not support multiple view types
        if (position == 0) {
            return nothingSelectedDropdownLayout == -1 ?
              new View(context) :
              getNothingSelectedDropdownView(parent);
        }

        // Could re-use the convertView if possible, use setTag...
        return adapter.getDropDownView(position - EXTRA, null, parent);
    }
     protected View getNothingSelectedDropdownView(ViewGroup parent) {
            return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
        }

}
</code></pre>

<p>This code i have taken it from here itself.
The problem I am facing is,as soon as the main activity loads,there are 2 toast msgs being displayed of the first item.
And when I'm selecting first element,the toast msg is displaying the 2nd element.Some problem in the index is occurring.
Plz help.</p>

<p>Thankss.</p>

<p>Mainactivity.java</p>

<pre><code>String[] countries = {"Dehradun","Solapur","Pune","Mumbai"};

String[] city = {"Dehradun","Solapur","Pune","Mumbai"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Spinner element
    final  Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    final  Spinner spinner1 = (Spinner) findViewById(R.id.spinner2);

      ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, countries);
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);

      spinner.setAdapter(new NothingSelectedSpinnerAdapter(dataAdapter,
             R.layout.contact_spinner_row_nothing_selected,this));
              // R.layout.contact_spinner_nothing_selected_dropdown, // Optional

      spinner.setOnItemSelectedListener(this);

      ArrayAdapter&lt;String&gt; dataAdapter1 = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, city);
      dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner1.setAdapter(dataAdapter1);

   spinner1.setAdapter(
      new NothingSelectedSpinnerAdapter(
              dataAdapter1,
              R.layout.contact_spinner_row_nothing_selected,
              // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
            this));
     spinner1.setOnItemSelectedListener(this);

public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    // On selecting a spinner item
    Spinner spinner = (Spinner)parent;
    Spinner spinner1 = (Spinner)parent;

    if(spinner.getId() == R.id.spinner1)
    {
        Toast.makeText(this, "Selected: " +countries[position] , Toast.LENGTH_SHORT).show();
    }

    if(spinner1.getId() == R.id.spinner2)
    {
        Toast.makeText(this, "Selected: " + city[position], Toast.LENGTH_SHORT).show();
    }

}
</code></pre>