2

Hola intento hacer un spinner en android con un adapter para un objeto CatalogoDetalle, para que al seleccionar la opción me devuelva el id de la opción. Utilizo el siguiente código

public class CatalogoDetalle {
    
    private int cad_idcatalogo;
    private String cad_nombreCatalogo;
    private int cad_idopcion;
    ...
    }

En el activity incio el spinner con

 List<CatalogoDetalle> tomadoDe; //los valores los traigo de la bd
    ..

      ArrayAdapter catAdapter = new ArrayAdapter<CatalogoDetalle>(this,   android.R.layout.simple_spinner_dropdown_item, tomadoDe);

        mySpinner.setAdapter(catAdapter);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // Get the value selected by the user
                // e.g. to store it as a field or immediately call a method
                CatalogoDetalle opcion = (CatalogoDetalle) parent.getSelectedItem();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

Sin embargo no se está mostrando correctamente.

En la lista me pone: com.example.compras.model.CatalogoDetalle que es el nombre del objeto. Muestro pantallazo aquí https://ibb.co/LrRFZg4

Espero puedan ayudarme a encontrar qué hice mal.

2
  • Si cambias esto: ArrayAdapter catAdapter = new ArrayAdapter<CatalogoDetalle>(this, android.R.layout.simple_spinner_dropdown_item, tomadoDe); por esto: ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, tomadoDe); ¿Te funciona? el 30 sep. 2021 a las 12:19
  • 1
    Gracias por la respuesta, ya logré solucionarlo con otra pregunta en este mismo sitio. La compartiría pero ya la perdí el 30 sep. 2021 a las 18:45

1 respuesta 1

2

Logré solucionarlo agregando en la creación del ArrayAdapter lo siguiente

 ArrayAdapter catAdapter = new ArrayAdapter<CatalogoDetalle>(this, android.R.layout.simple_spinner_dropdown_item, tomadoDe) {


            // And the "magic" goes here
            // This is for the "passive" state of the spinner
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
                TextView label = (TextView) super.getView(position, convertView, parent);
                label.setTextColor(Color.BLACK);
                // Then you can get the current item using the values array (Users array) and the current position
                // You can NOW reference each method you has created in your bean object (User class)
                CatalogoDetalle item = getItem(position);
                label.setText(item.getCad_descripcionesp());
               

                // And finally return your dynamic (or custom) view for each spinner item
                return label;
            }

            // And here is when the "chooser" is popped up
            // Normally is the same view, but you can customize it if you want
            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                TextView label = (TextView) super.getDropDownView(position, convertView, parent);
                label.setTextColor(Color.BLACK);
                CatalogoDetalle item = getItem(position);
                label.setText(item.getCad_descripcionesp());

                return label;
            }
        };  

Y funciona perfecto

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.