【问题标题】:How can I use custom Spinner class in spinner object to fire OnItemSelectedListener event every-time如何在微调器对象中使用自定义微调器类来每次触发 OnItemSelectedListener 事件
【发布时间】:2016-01-17 15:12:37
【问题描述】:

我的 Spinner 有一个 OnItemSelectedListener,但是当所选项目与前一个相同时,它不会被调用。我也实现了 OnClickListener,但它不起作用。我找到了一些解决方案。但我不知道我该如何使用它。我需要捕捉用户每次点击一个项目。

JAVA 代码:

spn_filter_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
        }

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

        }
    });

XML 代码:

<Spinner
            android:id="@+id/spn_filter_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

【问题讨论】:

  • it is not called when the selected item is the same as the previous one 究竟是什么意思。
  • 我想使用自定义 Spinner 类来做到这一点。我该如何实施? @meda

标签: java android listener android-spinner onitemselectedlistener


【解决方案1】:

如果您选择不同的项目,onItemSelected 会被调用。

如果您未选择其他项目,则表示您尚未选择项目,然后调用onNothingSelected

 spn_filter_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
        }

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

             CharSequence message = "called when the selected item is the same as the previous one";  
             Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
        }
    });

【讨论】:

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