21

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
Yogesh Nikam
  • 349
  • 1
  • 2
  • 11

3 Answers3

21

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 …

SqAR.org
  • 557
  • 6
  • 15
10

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!

Panos Gr
  • 667
  • 5
  • 13
-2

if you want to double binding,you can use @+{...} ,double binding must granted that value is unique,if the field's value is not unique,try @{...},remove '+'

Tom
  • 57
  • 1
  • 1