martes, 6 de marzo de 2018

Cargar un spinner con datos y mostrar un texto cuando se selecciones

package pauloescobar.com.co.mispinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    String[] dias = {"lunes", "martes","mercoles", "jueves","viernes","sabado","domingo"};
    private Spinner spinner;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initComponents();
        loadInformacion();
        spinnerSelectListener();

    }
    private void spinnerSelectListener(){
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),dias[i],Toast.LENGTH_SHORT).show();
            }


            @Override            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }
    private void loadInformacion() {

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this,R.layout.support_simple_spinner_dropdown_item,dias);
        spinner.setAdapter(arrayAdapter);

    }

    private void initComponents() {
        spinner = (Spinner)findViewById(R.id.miSpiner);
    }



}

No hay comentarios:

Publicar un comentario