1

I m having problems working with Spinner. I have a custom adapter for the Spinner. This is the code :

public class CategoriaArrayAdapter extends ArrayAdapter<Categoria> {

    private Context context;
    private ArrayList<Categoria> values;

    public CategoriaArrayAdapter(Context context, int textViewResourceId,
                       ArrayList<Categoria> values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;
    }

    public View myCustomView(int position, @Nullable View myView, @NonNull ViewGroup parent){
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.row_buscar_categoria_nothing_selected, parent, false);
        TextView textCategoria = customView.findViewById(R.id.tv_categoria_empty);
        textCategoria.setText(values.get(position).getNombre());
        return customView;
    }

    @Override
    public int getCount(){
        return values.size();
    }

    @Override
    public Categoria getItem(int position){
        return values.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return myCustomView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return myCustomView(position, convertView, parent);
    }
}

Yes, I made it in Java because I dont have too much experience with kotlin. Well, this is the implementation of my adapter:

    private fun eventSpinners(){

        myCustomAdapter = CategoriaArrayAdapter(context, R.layout.row_buscar_categoria_nothing_selected,categoriaArray)
        categoriaSpinner.adapter = myCustomAdapter

        categoriaSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                Log.d("BuscarActivity", "May not work")
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                Toast.makeText(context, "Works!", Toast.LENGTH_LONG).show()
            }

        }
    }

So, my problem is when the view is inflated, the Spinner doesn't show the first element of the ArrayList.

enter image description here

And when I click on an element of the array, nothing happens.

Any idea about what could be the problem?

Update:

This is my the xml of the row.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_categoria_empty"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:text="Seleccionar"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

And the spinner spinner is inside this block in the xml:

<android.support.constraint.ConstraintLayout
            android:id="@+id/constraint_linea"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="17dp"
            android:background="@drawable/shape_button_buscar"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/contrasint_edit_text">

            <Spinner
                android:id="@+id/spinner_linea"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:paddingLeft="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/divider"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>
Benjamin
  • 7,055
  • 6
  • 40
  • 60
LordCommanDev
  • 922
  • 12
  • 38

2 Answers2

0

Your spinner is probably getting populated, but the contents are not visible.

This is happening because in the XML for your item view, you used match_parent inside ContsraintLayout

Direct children of ContsraintLayout must use 0dp instead of match_parent.

Reference: Set width to match constraints in ConstraintLayout

vyashole
  • 79
  • 5
0

You should use the native layout for the item view

data class Category(val title: String)

class CategoryAdapter(context: Context, private var list: List<Category>) :
    ArrayAdapter<Category>(context, android.R.layout.simple_spinner_dropdown_item, list) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val textView =  super.getView(position, convertView, parent) as TextView

        textView.text = list[position].title

        return textView
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val textView =  super.getDropDownView(position, convertView, parent) as TextView

        textView.text = list[position].title

        return textView
    }
}