set Spinner Item - Android User Interface

Android examples for User Interface:Spinner

Description

set Spinner Item

Demo Code


import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Main {
  public static void setSpinnerItem(Spinner spinner, String value) {
    if (spinner != null && value != null) {
      if (spinner.getAdapter() instanceof ArrayAdapter) {
        ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner
            .getAdapter();//from  ww w  . j ava2  s  .c om
        for (int position = 0; position < adapter.getCount(); position++) {
          if (adapter.getItem(position).equals(value)) {
            spinner.setSelection(position);
            return;
          }
        }
      }
    }
  }
}

Related Tutorials