Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM : How to Concat the String in model class?

I created app using retrofit callback. In that i wanna show some information with text. In textView i already binded the data, also i need to concat some texts along with that.

My Code Follows

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 Output:

Model Return + "Concat String"

like image 781
Saiprashanth Avatar asked Mar 21 '17 11:03

Saiprashanth


2 Answers

Try this Simple Way

All your processing things and Business Logic should be in Controller Class (i.e: ViewModel in MVVM)

write one method in Controller, pass the value to Controller, then that method returns the value with Concat String

Code Sample

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

Controller Method

public String getUserProfile(String name)
{
     return name + "concat string";
}
like image 66
Keerthivasan Avatar answered Nov 03 '22 00:11

Keerthivasan


concat it in xml

android:text="@{UserProfile.username + `Concat String`}"
like image 27
Ravi Avatar answered Nov 02 '22 22:11

Ravi