MVVM: How to bind a string in a model class?

I created an application using a callback. In that I want to show some information with text. In the textView I have already bound the data, also I need to concatenate some texts along with this.

My code:

View:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="UserProfile"
            type="com.practical_session.sai.instaprouser.Profile.model.UserProfileInfo" />

    </data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear"
    android:orientation="vertical"
    android:background="@drawable/animation_list"
    tools:context="com.practical_session.sai.instaprouser.Profile.view.ProfileActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatTextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="8dp"
            android:textSize="20dp"
            android:text="@{UserProfile.username}"
            android:textColor="@android:color/black" />

</RelativeLayout>
</LinearLayout>
</layout>

      

Model:

public class UserProfileInfo extends BaseObservable {

    @SerializedName("username")
    @Expose
    private String username;

    @Bindable
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

      

Expected Result:

Returning the model + "Concatenated string"

+3


source to share


2 answers


Try this simple way

All your processing and business logic should be in a controller class (i.e. ViewModel in MVVM)

write one of the methods in Controller, pass the value to Controller, then this method returns the value using Concat string

Sample code



android:text="@{Controller.getUserProfile(UserProfile.username)}"

      

Controller method

public String getUserProfile(String name)
{
     return name + "concat string";
}

      

+3


source


enclose in xml



android:text="@{UserProfile.username + `Concat String`}"

      

+2


source







All Articles