Java tutorial
//package com.java2s; /* * jfabrix101 - Library to simplify the developer life * * Copyright (C) 2013 jfabrix101 (www.fabrizio-russo.it) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version * 2.1, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License 2.1 for more details. * * You should have received a copy of the GNU Lesser General Public * License version 2.1 along with this program. * If not, see <http://www.gnu.org/licenses/>. */ import java.util.List; import android.content.Context; import android.widget.ArrayAdapter; import android.widget.Spinner; public class Main { /** * Carica i dati di uno spinner con la lista passata * @param context - Context * @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); } }