2016-10-17 11 views
1

私はSpinnerで双方向バインディングを設定しようとしばらくしてきました。@InverseBindingAdapter/AttrChangedを使用したAndroid Spinnerの双方向バインディングは機能しません

ウェブ上やここではスタックオーバーフローに関する多くの例がありますが、どれも私のためには機能しません。

それはちょうど国のスピナーですが、私はこの方法でそれのための国のアダプタを定義しました:

@InverseBindingAdapter(attribute = "selectedCountry", event = "selectedCountryAttrChanged") 
public static String bindCountryInverseAdapter(AppCompatSpinner pAppCompatSpinner) { 
    Object selectedItem = pAppCompatSpinner.getSelectedItem(); 
    SpinnerAdapter adapter = pAppCompatSpinner.getAdapter(); 
    if (adapter instanceof CountrySpinnerAdapter) { 
     return (String) selectedItem; 
    } 
    throw new UnsupportedOperationException("The adapter must be a CountrySpinnerAdapter"); 
} 

@BindingAdapter(value = "selectedCountryAttrChanged", requireAll = false) 
public static void bindCountryChanged(AppCompatSpinner pAppCompatSpinner, final InverseBindingListener newTextAttrChanged) { 
    AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      newTextAttrChanged.onChange(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      newTextAttrChanged.onChange(); 
     } 
    }; 
    pAppCompatSpinner.setOnItemSelectedListener(listener); 

} 

@BindingAdapter("selectedCountry") 
public static void bindCountryValue(AppCompatSpinner pAppCompatSpinner, String newSelectedValue) { 
    SpinnerAdapter adapter = pAppCompatSpinner.getAdapter(); 
    if (adapter instanceof CountrySpinnerAdapter) { 
     ((CountrySpinnerAdapter) adapter).bindSelectedValue(pAppCompatSpinner, newSelectedValue); 
     return; 
    } 
    throw new UnsupportedOperationException("The adapter must be a CountrySpinnerAdapter"); 
} 

bindCountryChangedメソッドが呼び出されることはありません。

私も(他の例以下)このバリアントを試してみた:これはと呼ばれるが、newTextAttrChangedは常にnullです

@BindingAdapter(value = {"selectedCountry", "selectedCountryAttrChanged"}, requireAll = false) 
public static void bindCountryValueChanged(AppCompatSpinner pAppCompatSpinner, String newSelectedValue, final InverseBindingListener newTextAttrChanged) { 
    AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      newTextAttrChanged.onChange(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      newTextAttrChanged.onChange(); 
     } 
    }; 
    pAppCompatSpinner.setOnItemSelectedListener(listener); 

} 

レイアウト、部品結合:

<data> 
    <variable 
     name="customer" 
     type="my.package.CustomerBinding" /> 
</data> 

ウィジェット:

<android.support.v7.widget.AppCompatSpinner 
    android:id="@+id/editCountry" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:spinnerMode="dropdown" 
    app:adapter="@{customer.countrySpinnerAdapter}" 
    app:selectedCountry="@{customer.country}" /> 

country idがちょうどObservableField<String>countrySpinnerAdapter国のリストについてはBaseAdapterです。

アンドロイドのGradleプラグイン:

classpath 'com.android.tools.build:gradle:2.2.1' 

ツールのバージョン:

buildToolsVersion "24.0.2" 

そしてもちろん、データバインディングが有効になっている:

dataBinding { 
    enabled = true 
} 

newTextAttrChangedは常にnullである理由/ BindingAdapterは決してありませんコールド?私は間違って何をしていますか?

+0

'アプリでなければなりません:それは'でなければなりませんのでselectedCountryは= "@ {customer.countryは}" 'あなたは、' = '双方向のバインディングを欠落していませんapp:selectedCountry = "@ = {customer.country}" '...? – yennsarah

+0

イエスは正しいです!私は何とかそれを見落としました... '@ = {'と '@ {'?私は '= '形式を使用するはずですが、後者はいつも使っていますか? –

+0

私は答えを投稿します。 ;) – yennsarah

答えて

5

バインディング式に=がありません。このバインディングを双方向バインディングとして使用することをシステムに通知します。

app:selectedCountry="@{customer.country}" 

app:selectedCountry="@={customer.country}" 
+0

私はあなたの答えにまったく異議を唱えていません。本当に正しいですが、私は本当に理解したいです:[公式文書] [1]はこれについて何も言いません。 [その他の] [2]双方向データバインディングに関する記事もありません。これは通常のObservableFieldに必要ですか?これは、私は2つの方法のデータバインドを試み、私を困惑させたのは初めてです:)カスタムフィールドで始まるのはおそらく最も賢明な決定ではありませんでした [1] https://developer.android.com/topic/libraries/data -binding/index.html [2] https://medium.com/@fabioCollini/android-data-binding-f9f9d3afc761#.xmm1v68hu –

+0

[This](https:// medium。com/google-developers/android-data-binding-2-way-your-way-ccac20f6313#.ijqh87173)は、DataBindingライブラリの開発者George Mountの記事です。あなたはまた、媒体上の彼の他の記事をチェックアウトする必要があります:) – yennsarah

+0

私は、ありがとう、ありがとう...私はこの種のものは、 –

関連する問題