Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The expression cannot be inverted, so it cannot be used in a two-way binding

I am trying to implement two way binding in my project.

but I am getting error when use conditional statement in layout.

I have created model class model.java where getter setter defined.

model.java

public class Model {
    public String name;
    public string id;

public String getWebsite() {
            return website;
        }

public void setWebsite(String website) {
            this.name = website;
        }


public int getId() {
            return name;
        }

public void setId(String id) {
            this.id = id;
        }
}

Edittext in fragment_details.java

<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"
    tools:context=".view.fragment.CompanyDetailsFragment">

 <data>
        <variable
            name="modelData"
            type=".Model" />

    </data>
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@={modelData.id}"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="@={modelData.website ?? "NA"}"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

DetailsFragment.java

public class DetailsFragment extends Fragment {
    FragmentDetailsBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

    binding =DataBindingUtil.inflate(inflater,R.layout.fragment_details,null,false);
    View view = binding.getRoot();
    MyAccountDetailsViewModel viewModel = ViewModelProviders.of(this).get(MyAccountDetailsViewModel.class);

    getDetails();

        return view;
}

    private void getDetails() {
        //Server call and set value to user
        binding.setUser(detailsByUser);

    }

    private void updateDetails() {
        Log.d("newData", binding.getUser() + "");
    }
}

I want two-way binding so that I can update user details. But I am getting the following error message

aWebsiteJavaLangObjectNull) ? ("NA") : (userDataWebsite)) cannot be inverted, so it cannot be used in a two-way binding
like image 726
Yogesh Nikam Avatar asked Sep 11 '19 06:09

Yogesh Nikam


People also ask

What is 2way binding?

Two-way binding gives components in your application a way to share data. Use two-way binding to listen for events and update values simultaneously between parent and child components.

What is two-way binding Android?

Two-way Data Binding is a technique of binding your objects to your XML layouts so that the layout can send data to your binding object. This is compared to a “traditional” or “one-way” Data Binding setup, where data would only move from your binding object to the layout.

What is one way and two-way data binding Android?

Data binding in Android The answer is simple. Updating the views from the data source is a simple one-way binding. In that case, you'll only access data from the data source and update the layout. Two-way data binding is nothing but updating the data source if there are any changes in the layout and vice versa.


2 Answers

Try using your modelData.websitevariable as an Observable or a MutableLiveData with the default value of "NA" and then bind the variable to your xml like so:

xml binding

android:text="@={modelData.website}"

java observable

public ObservableField<String> website = new ObservableField<>();

After that, simply set your values to website as needed.

Hope this helped, good luck!

like image 199
Panos Gr Avatar answered Sep 21 '22 05:09

Panos Gr


Had a similar message but was using Kotlin, the way to fix it here was to use var instead of val for the bound value in the data class I was using. Hope this helps somebody …

like image 41
SqAR.org Avatar answered Sep 19 '22 05:09

SqAR.org