android - 数据绑定(bind)不更新 View

标签 android mvvm

我正在尝试更新我的 View 模型类中的 View ,但它不起作用。这是我的 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 更新 View 并且成功了。但是通过从 BaseObservable 扩展 MainViewModel 是行不通的

最佳答案

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

notifyPropertyChanged(BR.ordersCount); 

当 Notify any listeners 时,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" />

希望这对你有帮助。

关于android - 数据绑定(bind)不更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811436/

相关文章:

wpf - MVVM 中的绑定(bind) slider 值

android - 选择其他项目时显示第一个项目文本的微调器

java - 不显示 Android 应用程序的准确输出

c# - 如何使用MVVM/ICommand模式在WPF中正确实现BackgroundWorker

wpf - MVVM ViewModel 和 subview

c# - Prism 5 模块化 : how to add RegionName in DataTemplate of TabControl in ItemsControl

c# - 使用Caliburn.Micro的动画启动画面

android - 为什么应用程序服务重启,但无法重启?

android - 如何将条目插入到 SQLite 数据库中

java - stopService() 方法不会停止服务