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
.
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>