【问题标题】:onItemSelected doesn't workonItemSelected 不起作用
【发布时间】:2018-07-11 05:04:47
【问题描述】:

我在使用 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>

【问题讨论】:

  • 发布您的 XML 文件。
  • @Ankita xml 的 Activity 或 spinner 的行?
  • 旋转器块。
  • 通过获取 size 来检查您的 categoriaArray 是否包含值。
  • 是的,它有值。我使用 LogCat 进行检查

标签: android kotlin android-spinner


【解决方案1】:

您的微调器可能已被填充,但内容不可见。

发生这种情况是因为在您的项目视图的 XML 中,您在 ContsraintLayout 中使用了 match_parent

ContsraintLayout 的直接子代必须使用 0dp 而不是 match_parent

参考:Set width to match constraints in ConstraintLayout

【讨论】:

  • 在该行的xml中?
  • 是的,行的 XML。此外,其他任何地方。 ConsraintLayout 的任何直接子级都不能使用 match_parent。您应该改用 0dp。
  • 还是不行,连onItemSelected都不行
  • 有趣。使用日志语句检查 getView 和 getDropdownView 是否被调用。
  • 是的,它们工作正常,但 onItemSelectedListener 没有,我不知道为什么!
【解决方案2】:

您应该为项目视图使用本机布局

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多