2017-12-14 6 views
1

viewmodelクラスからビューを更新しようとしていますが、機能していません。ここに私のxmlファイルであるデータバインディングがビューを更新していません

<data> 

    <variable 
     name="mainViewModel" 
     type=".MainViewModel" /> 
</data> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/black" 
      android:elevation="4dp" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="4dp" 
      android:background="@color/base_color" /> 

     <TextView 
      android:id="@+id/personalNumber" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/text_border" 
      android:gravity="center_vertical" 
      android:padding="8dp" 
      android:text="@{String.valueOf(mainViewModel.personalNumber)}" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/location" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/text_border" 
      android:gravity="center_vertical" 
      android:padding="8dp" 
      android:text="@{mainViewModel.location}" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/ordersCount" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/text_border" 
      android:gravity="center_vertical" 
      android:padding="8dp" 
      android:text="@{mainViewModel.ordersCount}" 
      android:textSize="16sp" /> 

     <Button 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="30dp" 
      android:background="@drawable/main_activity_buttons" 
      android:textColor="@color/white" /> 
    </LinearLayout> 
</RelativeLayout> 
</layout> 

MainViewModelクラス

public class MainViewModel extends BaseObservable { 

private int ordersCount; 

@Bindable 
public int getPersonalNumber() { 
    return MyApplication.getClientPreferences().getPersonalNumber(); 
} 

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

@Bindable 
public String getOrdersCount(){ 
    return 

MyApplication.getInstance()getResources()はgetString(R.string.orders_count、ordersCount)。。。 }

public void setOrdersCount(int ordersCount) { 
    this.ordersCount = ordersCount; 
    notifyPropertyChanged(this.ordersCount); 
    Toast.makeText(MyApplication.getInstance(), String.valueOf(ordersCount), Toast.LENGTH_SHORT).show(); 
} 
} 

私はMainViewModelからのビューを更新するための観察可能なフィールドを使用しようとしましたが、それが働きました。しかし、それはあなたが

yourBindingObject.setMianViewModel(mainViewModelObj); 

と呼ばれていない可能性がありますBaseObservable

答えて

0

データを更新するために、プロパティの下に使用してください知っている:

notifyPropertyChanged(BR.ordersCount); 

リスナーに通知すると、BRは生成されたクラスであり、BR.ordersCountは実行時にTextviewに値を更新します。その内の任意のパッケージが、その後も、それはそれに絶対パスだが定義している場合

<variable 
     name="mainViewModel" 
     type="com.app.YourAppName.MainViewModel" /> 

はまた、以下のように定義します。

以下のようにも設定された値:

<TextView 
      android:id="@+id/ordersCount" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/text_border" 
      android:gravity="center_vertical" 
      android:padding="8dp" 
      android:text="@={mainViewModel.ordersCount}" 
      android:textSize="16sp" /> 

が、これはあなたを助け願っています。

+0

が働き、upvoteとそれにうまく答えるのに役立ちます。 –

+0

これが機能しました。実際に私はnotifyPropertyChangedメソッドを試しましたが、何とか私のプロパティを見つけることができませんでした。プロジェクトを再構築した後、それはうまくいった。 –

+0

素晴らしい素敵な仕事 –

0

からMainViewModelを拡張することで動作していない、これを試してみて、私は

+0

動作しませんでした。私はすでにバインディングオブジェクトにビューモデルを設定しています –

+0

あなたがモデルオブジェクトでnotifyallを呼び出すこともできます。 –

関連する問題