I want to get value from livedata and update the UI but it doesn't show any things when data received. what's the code's problem?

ProfileModel.java

public class ProfileModel {

    public  ObservableField<String> imageUrl = new ObservableField<>();
    public  ObservableField<String> name = new ObservableField<>();
    public  ObservableField<String> lastName = new ObservableField<>();
    public  ObservableField<String> problemDescription = new ObservableField<>();
    public  ObservableField<Date> birthDay = new ObservableField<>();

    public ProfileModel(String imageUrl,
                        String name,
                        String lastName,
                        String problemDescription,
                        Date birthDay) {
        this.imageUrl.set(imageUrl);
        this.name.set(name);
        this.lastName.set(lastName);
        this.problemDescription.set(problemDescription);
        this.birthDay.set(birthDay);
    }

    public ObservableField<String> getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(ObservableField<String> imageUrl) {
        this.imageUrl = imageUrl;
    }

    public ObservableField<String> getName() {
        return name;
    }

    public void setName(ObservableField<String> name) {
        this.name = name;
    }

    public ObservableField<String> getLastName() {
        return lastName;
    }

    public void setLastName(ObservableField<String> lastName) {
        this.lastName = lastName;
    }

    public ObservableField<String> getProblemDescription() {
        return problemDescription;
    }

    public void setProblemDescription(ObservableField<String> problemDescription) {
        this.problemDescription = problemDescription;
    }

    public ObservableField<Date> getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(ObservableField<Date> birthDay) {
        this.birthDay = birthDay;
    }

    @Override
    public String toString() {
        return "ProfileModel{" +
                "imageUrl=" + imageUrl.get() +
                ", name=" + name.get() +
                ", lastName=" + lastName +
                ", problemDescription=" + problemDescription +
                ", birthDay=" + birthDay +
                '}';
    }
}

ProfileViewModel.java

public class ProfileViewModel  extends BaseViewModel<ProfileNavigator> {

    private MutableLiveData<ProfileModel> profileModelMutableLiveData;

    public ProfileViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) {
        super(dataManager, schedulerProvider);
        profileModelMutableLiveData = new MutableLiveData<>();
        fetchRepos();
    }

    public void fetchRepos() {
        setIsLoading(true);
        getCompositeDisposable().add(getDataManager()
                .getProfileModel()
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(profileModel -> {
                    Log.d("livedata",profileModel.toString());
                    profileModelMutableLiveData.setValue(profileModel);
                    setIsLoading(false);
                }, throwable -> {
                    setIsLoading(false);
                    getNavigator().handleError(throwable);
                }));
    }

    public LiveData<ProfileModel> getProfileModelMutableLiveData() {
        return profileModelMutableLiveData;
    }


    public void onPremiumClick(){

    }
}

BaseFragment.java

public abstract class BaseFragment<T extends ViewDataBinding, V extends BaseViewModel> extends Fragment {

    protected FragmentNavigation mFragmentNavigation;
    private BaseActivity mActivity;
    private View mRootView;
    private T mViewDataBinding;
    private V mViewModel;

    public abstract int getBindingVariable();

    public abstract
    @LayoutRes
    int getLayoutId();

    public abstract V getViewModel();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) context;
            this.mActivity = activity;
            activity.onFragmentAttached();
        }
        if (context instanceof FragmentNavigation) {
            mFragmentNavigation = (FragmentNavigation) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        performDependencyInjection();
        super.onCreate(savedInstanceState);
        mViewModel = getViewModel();
        setHasOptionsMenu(false);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mViewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        mRootView = mViewDataBinding.getRoot();
        return mRootView;
    }

    public BaseActivity getBaseActivity() {
        return mActivity;
    }

    public T getViewDataBinding() {
        return mViewDataBinding;
    }

    public void hideKeyboard() {
        if (mActivity != null) {
            mActivity.hideKeyboard();
        }
    }

    public boolean isNetworkConnected() {
        return mActivity != null && mActivity.isNetworkConnected();
    }

    public void openActivityOnTokenExpire() {
        if (mActivity != null) {
            mActivity.openActivityOnTokenExpire();
        }
    }

    private void performDependencyInjection() {
        AndroidSupportInjection.inject(this);
    }

    public interface Callback {

        void onFragmentAttached();

        void onFragmentDetached(String tag);
    }
}



public class ProfileFragment
        extends BaseFragment<FragmentProfileBinding,ProfileViewModel> implements ProfileNavigator {

    @Inject
    ViewModelProviderFactory factory;

    FragmentProfileBinding mFragmentProfileBinding;
    ProfileViewModel mProfileViewModel;


    public ProfileFragment() {
        // Required empty public constructor
    }



    public static ProfileFragment newInstance() {
        ProfileFragment fragment = new ProfileFragment();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getBindingVariable() {
        return BR.viewModel;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_profile;
    }

    @Override
    public ProfileViewModel getViewModel() {
        mProfileViewModel= ViewModelProviders.of(this, factory).get(ProfileViewModel.class);
        return mProfileViewModel;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProfileViewModel.setNavigator(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mFragmentProfileBinding = getViewDataBinding();

    }

    @Override
    public void handleError(Throwable throwable) {

    }
}



<?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="viewModel"
            type="ir.basamadazmanovin.heartrate.ui.main.profile.ProfileViewModel" />

    </data>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/activity_login_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:refreshing="@{viewModel.isLoading}">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/md_grey_200">

                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView2"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    app:layout_constraintBottom_toTopOf="@+id/guideline18"
                    app:layout_constraintEnd_toStartOf="@+id/guideline13"
                    app:layout_constraintStart_toStartOf="@+id/guideline12"
                    app:layout_constraintTop_toTopOf="@+id/guideline17">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="8dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            android:text="Free Account"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/guideline21"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                        <androidx.constraintlayout.widget.Guideline
                            android:id="@+id/guideline21"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            app:layout_constraintGuide_percent="0.5" />

                        <mehdi.sakout.fancybuttons.FancyButton
                            android:id="@+id/activity_login_login_btn"
                            android:layout_width="124dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginEnd="8dp"
                            android:onClick="@{()-> viewModel.onPremiumClick()}"
                            android:paddingStart="16dp"
                            android:paddingTop="8dp"
                            android:paddingEnd="16dp"
                            android:paddingBottom="8dp"
                            app:fb_borderColor="#FFFFFF"
                            app:fb_borderWidth="1dp"
                            app:fb_defaultColor="@color/button_color"
                            app:fb_focusColor="@color/button_color"
                            app:fb_radius="@dimen/button_radius"
                            app:fb_text="Premium"
                            app:fb_textColor="#FFFFFF"
                            app:fb_textSize="@dimen/button_textSize"
                            app:layout_constraintBottom_toBottomOf="@+id/textView2"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="@+id/guideline21"
                            app:layout_constraintTop_toTopOf="@+id/textView2" />


                    </androidx.constraintlayout.widget.ConstraintLayout>
                </androidx.cardview.widget.CardView>

                <View
                    android:id="@+id/view2"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:background="@color/material_blue400"
                    android:visibility="visible"
                    app:layout_constraintBottom_toTopOf="@+id/guideline19"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.675" />

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/circleImageView"
                    android:layout_width="124dp"
                    android:layout_height="124dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:src="@mipmap/ic_launcher_round"
                    app:civ_border_color="@color/material_white"
                    app:civ_border_width="2dp"
                    app:layout_constraintBottom_toTopOf="@+id/guideline17"
                    app:layout_constraintEnd_toStartOf="@+id/guideline20"
                    app:layout_constraintStart_toStartOf="@+id/guideline20"
                    app:layout_constraintTop_toTopOf="@+id/guideline14"
                    app:layout_constraintVertical_bias="0.358" />

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:text="@{viewModel.getProfileModelMutableLiveData.name}"
                    android:textColor="@color/material_white"
                    app:layout_constraintBottom_toTopOf="@+id/guideline17"
                    app:layout_constraintEnd_toEndOf="@+id/circleImageView"
                    app:layout_constraintStart_toStartOf="@+id/circleImageView"
                    app:layout_constraintTop_toBottomOf="@+id/circleImageView" />

                <androidx.cardview.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="@+id/cardView2"
                    app:layout_constraintStart_toStartOf="@+id/cardView2"
                    app:layout_constraintTop_toTopOf="@+id/guideline18">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/textInputLayout"
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="8dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="First Name" />
                        </com.google.android.material.textfield.TextInputLayout>

                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/textInputLayout2"
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="4dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
                            app:layout_constraintStart_toStartOf="@+id/textInputLayout"
                            app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="Last Name" />
                        </com.google.android.material.textfield.TextInputLayout>

                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/textInputLayout5"
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="4dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/textInputLayout4"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="Year" />
                        </com.google.android.material.textfield.TextInputLayout>

                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/textInputLayout4"
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/textInputLayout3"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintStart_toEndOf="@+id/textInputLayout5"
                            app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="Month" />
                        </com.google.android.material.textfield.TextInputLayout>

                        <com.google.android.material.textfield.TextInputLayout
                            android:id="@+id/textInputLayout3"
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="4dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintStart_toEndOf="@+id/textInputLayout4"
                            app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="Day" />
                        </com.google.android.material.textfield.TextInputLayout>

                        <com.google.android.material.textfield.TextInputLayout
                            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:layout_marginBottom="8dp"
                            app:errorEnabled="true"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="@+id/textInputLayout3"
                            app:layout_constraintStart_toStartOf="@+id/textInputLayout5"
                            app:layout_constraintTop_toBottomOf="@+id/textInputLayout4"
                            app:layout_constraintVertical_bias="0.0">

                            <com.google.android.material.textfield.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="128dp"
                                android:hint="Describe Your Problems Here" />
                        </com.google.android.material.textfield.TextInputLayout>

                    </androidx.constraintlayout.widget.ConstraintLayout>
                </androidx.cardview.widget.CardView>

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_begin="8dp" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_end="8dp" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_begin="8dp" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.5" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline17"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.3" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline18"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.4" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline19"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.35" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline20"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.5" />

                <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guideline22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.45" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>

android:text="@{viewModel.getProfileModelMutableLiveData.name}"

this is where i want to change when the data received but it doesn't show the value even though the livedata has value

Answer

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions