Example usage for android.widget Spinner setSelection

List of usage examples for android.widget Spinner setSelection

Introduction

In this page you can find the example usage for android.widget Spinner setSelection.

Prototype

@Override
    public void setSelection(int position) 

Source Link

Usage

From source file:Main.java

public static void setSpinnerSelectionWithIndex(View view, int spinnerId, int index) {
    Spinner spinner = (Spinner) view.findViewById(spinnerId);
    spinner.setSelection(index);
}

From source file:Main.java

public static void setSpinnerSelection(Spinner spinner, int position) {
    if (spinner.getSelectedItemPosition() != position) {
        spinner.setSelection(position);
    } else {/*from w  ww .  j av a  2s . co  m*/
        spinner.getOnItemSelectedListener().onItemSelected(spinner, spinner.getSelectedView(), position,
                spinner.getAdapter().getItemId(position));
    }
}

From source file:Main.java

public static void setSpinnerSelectionWithString(View view, int spinnerId, String text) {
    Spinner spinner = (Spinner) view.findViewById(spinnerId);

    int count = spinner.getCount();
    for (int i = 0; i < count; i++) {
        if (spinner.getItemAtPosition(i).toString().equals(text))
            spinner.setSelection(i);
    }// w w  w . j a  v  a  2  s  . c o  m
}

From source file:Main.java

public static void setSpinnerSelectionById(Spinner spinner, int itemId) {
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) spinner.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if (adapter.getItemId(position) == itemId) {
            spinner.setSelection(position);
            return;
        }//from   w  w  w .  jav  a2s. c  o m
    }
    spinner.setSelection(0);
}

From source file:Main.java

/**
 * Carica i dati di uno spinner con la lista passata
 * @param context - Context/*from   ww  w. j  a  v a  2s.c  o  m*/
 * @param spinner - Spinner da caricare
 * @param data - List di stringhe con cuoi caricare lo spinner
 * @param selectedPosition - Posizione di selezione (minimo zero)
 */
public static void setSpinnerData(Context context, Spinner spinner, List<String> data, int selectedPosition) {
    if (selectedPosition < 0)
        selectedPosition = 0;
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
            data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setSelection(selectedPosition);
}

From source file:com.ada.utils.Ui.java

public static void setSelection(Spinner spinner, String selection) {
    final int count = spinner.getCount();
    for (int i = 0; i < count; i++) {
        String item = spinner.getItemAtPosition(i).toString();
        if (item.equalsIgnoreCase(selection)) {
            spinner.setSelection(i);
        }//from w  ww. j  a v a  2 s .  c  om
    }
}

From source file:mobile.tiis.appv2.helpers.Utils.java

public static void setSpinnerSelection(Spinner spinner, String value) {
    @SuppressWarnings("unchecked")
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter();
    int spinnerPosition = adapter.getPosition(value);
    spinner.setSelection(spinnerPosition);
}

From source file:org.totschnig.myexpenses.dialog.DialogUtils.java

public static Spinner configureEncoding(View view, Context context, String prefName) {
    Spinner spinner = (Spinner) view.findViewById(R.id.Encoding);
    spinner.setSelection(
            Arrays.asList(context.getResources().getStringArray(R.array.pref_qif_export_file_encoding))
                    .indexOf(MyApplication.getInstance().getSettings().getString(prefName, "UTF-8")));
    return spinner;
}

From source file:org.totschnig.myexpenses.dialog.DialogUtils.java

public static Spinner configureDelimiter(View view, Context context, String prefName) {
    Spinner spinner = (Spinner) view.findViewById(R.id.Delimiter);
    spinner.setSelection(
            Arrays.asList(context.getResources().getStringArray(R.array.pref_csv_import_delimiter_values))
                    .indexOf(MyApplication.getInstance().getSettings().getString(prefName, ",")));
    return spinner;
}

From source file:com.ultramegasoft.flavordex2.util.EntryFormHelper.java

/**
 * Set up a Spinner with an extra field.
 *
 * @param spinner The Spinner/*from  ww  w  .ja v a 2 s  . c  om*/
 * @param extra   The extra field to associate with the View
 */
protected static void initSpinner(@NonNull Spinner spinner, @Nullable final ExtraFieldHolder extra) {
    if (extra == null) {
        return;
    }
    if (extra.value == null) {
        extra.value = "0";
    }
    spinner.setSelection(Integer.valueOf(extra.value));

    final AdapterView.OnItemSelectedListener listener = spinner.getOnItemSelectedListener();

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            extra.value = position + "";
            if (listener != null) {
                listener.onItemSelected(parent, view, position, id);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            if (listener != null) {
                listener.onNothingSelected(parent);
            }
        }
    });
}