Show Selected item of a dynamic spinner with a Toast on android

0

I have a text edit and a button that add values to an adapter and it is shown in a listView and a spinner, I want to show in a message the selected element, here my code:

public class MainActivity extends Activity
{
    EditText txt;
    Button save;
    Spinner opcion;

    ArrayList<String> addArray = new ArrayList<String>();

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

        txt = (EditText)findViewById(R.id.txtInput);
        save = (Button)findViewById(R.id.btnInput);
        opcion =(Spinner)findViewById(R.id.spOpcion);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, addArray);
        opcion.setAdapter(adapter);
        opcion.setPrompt("Selecciona una opción");

        save.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String getInput = txt.getText().toString();

                if(addArray.contains(getInput))
                {
                    Toast.makeText(getBaseContext(), "El Elemento ya se había agregado.", Toast.LENGTH_SHORT).show();
                }
                else if(getInput.trim().equals(""))
                {
                    Toast.makeText(getBaseContext(), "Elemento vacío, ingrese texto antes de guardar.", Toast.LENGTH_LONG).show();
                }

                else if(!addArray.contains(getInput))
                {
                    addArray.add(getInput);
                    ((EditText)findViewById(R.id.txtInput)).setText("");
                }
            }
        });

        opcion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
            {
                Toast.makeText(getBaseContext(), parentView.getItemAtPosition(position).toString() , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView)
            {
                //Nada seleccionado
            }
        });
    }
}

    
asked by Kuroi 14.04.2018 в 21:46
source

2 answers

1

With the onItemSelected method you get the position ( position ) in int and you get the string that is in that position with getSelectedItem().toString() :

opcion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), "El Elemento seleccionado es posición número: "+position +" El String es: "+opcion.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    
answered by 15.04.2018 / 00:19
source
1

after opcion.setSelection(0); add this code:

opcion.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
         Toast.makeText(getApplicationContext(), parentView.getItemAtPosition(position).toString() , Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
         //Nada seleccionado
        }

    });
    
answered by 15.04.2018 в 08:13