I have a ViewModel class, and a Model class in that.
class WorksheetViewModel {
private Worksheet worksheet;
public Worksheet getWorksheet() {
return worksheet;
}
public void setWorksheet(Worksheet worksheet) {
this.worksheet = worksheet;
if (worksheet != null) showButtons();
Logger.E(worksheet.getShort_text());
}
this is working here: Logger.E(worksheet.getShort_text());
and in my xml
<import type="android.view.View" />
<variable
name="viewModel"
type="viewmodels.WorksheetViewModel" />
..
and a textview with:
android:text="@{viewModel.worksheet.short_text + `?`}"
My probleme is, viewModel.worksheet.short_text is allways null. Why?
binding.setViewModel(new WorksheetViewModel());
binding.executePendingBindings();
Also I have all the
Worksheet extends BaseObservable
and
@Bindable
public String getShort_text() {
return short_text;
}
in my Worksheet
Problem is you are just setting ViewModel in binding binding.setViewModel(new WorksheetViewModel());
but WorksheerViewModel
also have Worksheet
object, and it is blank.
So this should be
WorksheetViewModel viewModel = new WorksheetViewModel();
viewModel.setWorksheet(new WorkSheet('short text value'))
binding.setViewModel(viewModel);
If any one get error in binding.setViewmodel()
it resolve by binding.setViewmodel()
, In setViewmodel
, Viewmodel
is variable name which is given in layout.
e.g
<data>
<variable
name="userviewmodel"
type="com.example.mvvmtest.viewmodel.LoginViewModel" />
</data>
Than use, binding.setUserviewmodel()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With