2017-08-11 14 views
1

アンドロイドデータバインディングでMVVMパターンを使用しようとしています。 私は、edittextフィールドに、LocationTypeモデルオブジェクトでobservablefieldを介してモデルにバインドして入力すると、同じテキストを表示するedittextとtextviewを持っています。Android双方向データバインディング、テキストビューが更新されない

私は、アプリケーションを起動すると、両方のフィールドが "hello"に設定されています。 しかし、edittextfieldをタイプすると、textviewは更新されません。モデルオブジェクトは、デバッグで見られるように正しく更新されます。

私が使用:モデルとちょうどいくつかのテキストに設定し、このフィールドを使用してXMLを更新を使用して

observablefield<String> 

ドロップ。意図したとおりに動作します。

モデル:

public class LocationType { 
private String locationType; 

public String getLocationType() { 
    return locationType; 
} 

public void setLocationType(String locationType) { 
    this.locationType = locationType; 

} 
} 

モデルビュー

public class LocationTypeViewModel extends BaseObservable { 
    @Bindable 
    private ObservableField<LocationType> locationTypeObservableField = new ObservableField<>(); 
    private Context context; 
    LocationType locationType; 

    public LocationTypeViewModel(Context context) { 
    this.context = context; 

    locationType = new LocationType(); 
    locationType.setLocationType("Hello"); 
    locationTypeObservableField.set(locationType); 


    } 

    public ObservableField<LocationType> getLocationTypeObservableField() { 
    Log.d("CALLED GET", locationType.getLocationType()); 
    return locationTypeObservableField; 
    } 


}  

XML:

<layout xmlns:app="http://schemas.android.com/apk/res-auto"> 

<data> 

    <import type="android.view.View"/> 

    <variable 
     name="viewModel" 
     type="fragment.LocationType.viewmodel.LocationTypeViewModel"/> 
</data> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 


    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:text="@{viewModel.locationTypeObservableField.locationType}" 
     android:id="@+id/edittext1"/> 

    <TextView 
     android:layout_below="@+id/edittext1" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:text="@{viewModel.locationTypeObservableField.locationType}" 
     android:id="@+id/text" 
     /> 




</RelativeLayout> 
</layout> 

答えて

0

問題は、あなたが観察されているC LocationTypeオブジェクトを指すLocationTypeViewModel内のフィールドにハングアップします。これは、オブジェクト参照が変更されたときにのみ変更を通知します。あなたが望むのは実際のStringフィールドのオブザーバーです。

また、EditTextに重要な2つの綴り記号がありません。 @{obj.fieldName}の代わりに@={obj.fieldName}を使用します。 @ =変更がソースに戻って伝播することを示す信号。ここで

はあなたのコードが意図したとおりに動作するように編集されている:

public class LocationType { 

    private String location; 

    public LocationType(String location) { 
     this.location = location; 
    } 

    public String getLocation() { 
     return location; 
    } 

    public void setLocation(String location) { 
     this.location = location; 
    } 
} 

public class LocationTypeViewModel extends BaseObservable { 

    private final LocationType locationType; 

    public LocationTypeViewModel() { 
     locationType = new LocationType("Hello"); 
    } 

    @Bindable 
    public String getLocation() { 
     return locationType.getLocation(); 
    } 

    public void setLocation(String location) { 
     locationType.setLocation(location); 
     notifyPropertyChanged(BR.location); 
    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <data> 

     <import type="android.view.View"/> 

     <variable 
      name="viewModel" 
      type=".LocationTypeViewModel"/> 
    </data> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 


     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:text="@={viewModel.location}" 
      android:id="@+id/edittext1"/> 

     <TextView 
      android:layout_below="@+id/edittext1" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:text="@{viewModel.location}" 
      android:id="@+id/text" 
      /> 

    </RelativeLayout> 
</layout> 
+0

ありがとうございます!私はどのように失われたのか分からない前に@を持っていました。しかし、私はモデルをPOJOとしてどのように持つことができますか? – Morti

+0

LocationTypeオブジェクトにBaseObservableを拡張させることができます。その後、フィールドを変更する際には、BR生成クラスから正しいIDを通知する必要があります。ドキュメントのObservable Objectsをチェックしてください:https://developer.android.com/topic/libraries/data-binding/index.html。そこには相対的な例があります。 –

+0

MVVMパターンを設計するときに行うべきことはありますか?ほとんどの例では、ViewModelはBaseObservableを拡張したものです。 – Morti

1

アンドロイド:テキスト= "@ = {viewModel.locationTypeObservableField.locationType}"

あなたが "=" .checkを追加するのを忘れと上記の行とコードを比較してください。

関連する問題