【问题标题】:Spinner AdapterView.OnItemSelectedListener not workingSpinner AdapterView.OnItemSelectedListener 不工作
【发布时间】:2017-02-22 01:59:16
【问题描述】:

我在编辑活动上使用了两种方式的数据绑定,我试图避免样板化,并且我需要知道该活动的数据何时更改。我为 EditText 创建了一个 TextWatcher,效果很好。现在我想用 Spinner 做同样的事情。所以我创建了一个 SpinnerWatcher 类,在我的模型视图 (POJO) 和布局中引用了它。一切都编译并运行,但我没有得到预期的结果。线

hasDataChanged = true;

永远不会被执行。我想在布局文件中指定 setOnItemSelectedListener,在 ViewModel 中指定逻辑,而不是在 Activity 中。

这是我的 SpinnerWatcher 类:

public abstract class MySpinnerWatcher implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public abstract void onItemSelected(AdapterView newValue);
}

这是我的视图模型:

public class Diver extends BaseObservable {
...
@Bindable
public AdapterView.OnItemSelectedListener getOnSpinnerChanged() {
    return new MySpinnerWatcher() {
        @Override
        public void onItemSelected(AdapterView newValue) {
            // This never gets executed
            hasDataChanged = true;
        }
    };
}
}

这是我的布局:

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinnerGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/gender_arrays"
android:prompt="@string/gender_prompt"
app:setOnItemSelectedListener="@{diverdb.getOnSpinnerChanged}"
style="@style/Widget.AppCompat.Spinner.Underlined"/>

知道如何在 Spinner 中更改值时触发 onItemSelected() 吗?

【问题讨论】:

    标签: android spinner onitemselectedlistener


    【解决方案1】:

    我能够修复我的代码。

    这是我的 Spinner Watcher 类:

    public class MySpinnerWatcher implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
    

    }

    这是我的视图模型:

    @Bindable
    public AdapterView.OnItemSelectedListener getOnSpinnerChangedDiveType() {
        return new MySpinnerWatcher() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                super.onItemSelected(parent, v, position, id);
                // The Hint is in the list at item 0
                if (position != 0 && position + 1 != mDiveTypePosition){
                    mHasDataChanged = true;
                    // The Hint is in the list at item 0
                    DiveType diveType = (DiveType) parent.getAdapter().getItem(position + 1);
                    mDiveType = diveType.getDiveType();
                }
            }
        };
    }
    

    这是我的布局:

    <fr.ganfra.materialspinner.MaterialSpinner
                    android:id="@+id/spinnerDiveType"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:selectedItemPosition="@={dive.diveTypePosition}"
                    app:ms_floatingLabelColor="@color/blue"
                    app:ms_multiline="true"
                    app:ms_enableFloatingLabel="true"
                    app:ms_enableErrorLabel="false"
                    app:ms_floatingLabelText="Dive Type"
                    app:ms_hint="Dive Type"
                    app:setOnItemSelectedListener="@{dive.getOnSpinnerChangedDiveType}"
                    app:adapter="@{dive.AdapterDiveType}"
                    />
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 2018-07-22
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多