Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data binding is not updating view

Tags:

android

mvvm

I am trying to update a view from my viewmodel class but it is not working. Here is my xml file

<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 class

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();
}
}

I tried to use Observable field for updating view from MainViewModel and it worked. But it is not working by extending MainViewModel from BaseObservable

like image 366
Ali güvenç Avatar asked Jan 29 '23 00:01

Ali güvenç


2 Answers

Please use below property to update data:

notifyPropertyChanged(BR.ordersCount); 

When Notify any listeners, BR is a generated class and BR.ordersCount update your value into Textview at runtime.

Also define like below:

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

if in that any package is there then also define it's absolute path into it.

Also set value like below:

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

hope this helped you.

like image 105
Jyubin Patel Avatar answered Jan 31 '23 19:01

Jyubin Patel


you might not have called

yourBindingObject.setMainViewModel(mainViewModelObj);

try this and let me know

like image 37
Harsh Patel Avatar answered Jan 31 '23 19:01

Harsh Patel