我的Spinner有一个OnItemSelectedListener,但是当所选项与前一个项相同时,它不会被调用。我也实现了OnClickListener,但它没有工作。我找到了一些解决方案。但我不知道如何使用它。每次用户点击某个项目时我都需要抓住。
JAVA CODE:
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 CODE:
<Spinner
android:id="@+id/spn_filter_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
答案 0 :(得分:0)
如果您选择了另一个项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();
}
});