【问题标题】:data binding is not updating view数据绑定不更新视图
【发布时间】:2017-12-14 10:46:53
【问题描述】:

我正在尝试从我的视图模型类更新视图,但它不起作用。这是我的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();
}
}

我尝试使用 Observable 字段从 MainViewModel 更新视图并且它有效。但是从 BaseObservable 扩展 MainViewModel 不起作用

【问题讨论】:

    标签: android mvvm


    【解决方案1】:

    请使用以下属性更新数据:

    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" />
    

    希望这对您有所帮助。

    【讨论】:

    • 如果这对您有帮助,请点赞并成功回复。
    • 这行得通。实际上我尝试了 notifyPropertyChanged 方法,但不知何故我找不到我的财产。重建项目后它工作了。
    • 在哪里调用 notifyPropertyChanged()?
    • @filthy-wizard 每当您更新 Textview 时,然后使用您的模型和那些 likned 模型并设置它,然后 notifyPropertyChanged() 用于更新它。
    【解决方案2】:

    你可能没有打电话

    yourBindingObject.setMainViewModel(mainViewModelObj);
    

    试试这个然后告诉我

    【讨论】:

    • 它没有用。我已经将 viewmodel 设置为绑定对象
    • 你也可以在你的模型对象上调用 notifyall 它会起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多