gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:18 25 4
gpt4 key购买 nike

我正在尝试更新我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com