我在使用Spinner
时遇到问题。我有一个Spinner
的自定义适配器。这是代码:
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);
}
}
是的,我用Java编写它是因为我对kotlin的经验不足。 好吧,这是我的适配器的实现:
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()
}
}
}
所以,我的问题是,当视图膨胀时,Spinner
不显示
ArrayList
的第一个元素。
当我单击数组的元素时,什么也没有发生。
关于可能是什么问题的任何想法?
更新:
这是我的行的xml。
<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>
微调器微调器位于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>
答案 0 :(得分:0)
您的微调框可能正在填充,但是其中的内容不可见。
之所以会这样,是因为在您的商品视图的XML中,您在match_parent
内使用了ContsraintLayout
ContsraintLayout
的直接子代必须使用0dp而不是match_parent
。
答案 1 :(得分:0)
您应在项目视图中使用本机布局
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
}
}