2015-12-15 9 views
9

私はAndroidのデータバインディングライブラリを使用しています。私はデータオブジェクトをBaseObservableまで延長しています。Android:データバインディング、notifyPropertyChanged()が機能していませんか?

public static class SimpleData extends BaseObservable implements Serializable { 
    private String text, subText; 
    private SpannableString totalText; 
@Bindable 
    public SpannableString getTotalText() { 
     return totalText; 
    } 

    public void setTotalText(SpannableString totalText) { 
     this.totalText = totalText; 
     notifyPropertyChanged(BR.totalText); 
    } 
} 

そして、私のXMLは

<TextView 
       android:id="@+id/patient_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:layout_marginLeft="16dp" 
       android:layout_toRightOf="@+id/patient_image" 
       android:textColor="@color/primary_text" 
       android:text="@{object.getTotalText()}" 
       /> 

結合は、初期値のために行われるにもバインドされます。しかし、値を変更すると、値を変更すると、変更がテキストビューに反映されません。

object.setTotalText(someSpannableString); 

何が問題なの?

+0

問題を解決できますか? –

+0

@mahdipishguy:いいえ。問題はまだ解消されていません。 – Ashwin

答えて

6

getterのフィールド名を使用します。

<TextView 
       android:id="@+id/patient_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:layout_marginLeft="16dp" 
       android:layout_toRightOf="@+id/patient_image" 
       android:textColor="@color/primary_text" 
       android:text="@{object.totalText}"/> 
+0

が間違っているようですが、 'BR'クラスを使用してフィールドを識別してください。setterとgetterの両方に' @Bindable' – LambergaR

1

私はちょうど同じ問題がありました。私の拘束力は初めて働き、2度目は働かないだろう。

私は@Bindableを私のセッターに置き、私のゲッターメソッドではないことを除いて、あなたと同じ設定をしました。

@Bindableを私のセッターとゲッターの両方に追加すると、この問題が解決しました。

私は図書館のデータバインディングの内側の労働者をデバッグするとき、私 はそれがフィールドが更新されたかどうかを確認する操作をしたかどうか からのため、要求を再バインドと呼ばれる方法が 呼び出されていなかったことに気づい最後の値。私の推測では、 メソッドの注釈が必要なので、 が再バインドする必要があるかどうかを確認するためにこの内部確認を行うことができます。

これが本当であるかどうかはわかりませんが、どちらの方法でも注釈を付けると問題が解決しました。データバインディングライブラリのドキュメントを見ると、ゲッターに注釈が表示されているだけです。

あなたは試すことができます:それはそれを解決するかどうか

@Bindable 
public SpannableString getTotalText() { 
    return totalText; 
} 

@Bindable 
public void setTotalText(SpannableString totalText) { 
    this.totalText = totalText; 
    notifyPropertyChanged(BR.totalText); 
} 

を参照してください。

関連する問題