Reputation:
I am programming an android-App and I am using the MVVM-Pattern. I have problems seeing the data in the layout in my view. I am new to android programming, so maybe i am thinking the wrong way. In C# i had no problems creating an MVVMC-App. To be honest, i do not understand how to show the contents in the layouts in the activity :-( This is my code in the view:
@Override
protected void onCreate(Bundle savedInstanceState) {
//This is the code which shows the white window with my data
super.onCreate(savedInstanceState);
ContentSelectItemBinding binding = DataBindingUtil.setContentView(this, R.layout.content_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
//when i add this, it will just show the empty view
setContentView(R.layout.activity_select_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
And thats how my easy SelectItemViewModel looks like
public TestItem item;
public SelectItemViewModel() {
item = new TestItem("Test", "User");
}
public List<TestItem> getItemList() {
return this.aTestItemList;
}
And here are my XML-Files:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="private.testapp.view.SelectItemActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_select_item" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
This is my layout file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="private.testapp.model.TestItem"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:textSize="128sp"
android:layout_height="wrap_content"
android:text="@{item.name}"/>
<TextView android:layout_width="wrap_content"
android:textSize="128sp"
android:layout_height="wrap_content"
android:text="@{item.description}"/>
</LinearLayout>
</layout>
All i want to do first is just display the testitem... Since it displays it when i just show the layout, i am sure that the binding and my viewmodel works. I don`t understand how the data in the layout will be shown in my view.
Upvotes: 2
Views: 3287
Reputation: 918
Option 1- forward variable from activity xml to included xml:
First,
ContentSelectItemBinding binding = DataBindingUtil.setContentView(this,R.layout.content_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
//when i add this, it will just show the empty view
setContentView(R.layout.activity_select_item);
You're setting your activity content twice which is wrong, replace it with just:
DataBindingUtil.setContentView(this,R.layout.activity_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
To set your activity xml as your layout.
Next, your activity_select_item xml should also be wrapped with a <layout>
tag and a <data>
tag inside to denote this is a data binding layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="private.testapp.model.TestItem"/>
</data>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="private.testapp.view.SelectItemActivity">
.
.
.
</layout>
Finally, forward the variable to your included layout inside the <include>
tag:
.
.
.
<include
layout="@layout/content_select_item"
app:item="@{item}"/>
.
.
.
</android.support.design.widget.CoordinatorLayout>
Option 2- find included view and bind it
First, mark included view with id:
<include
id="@+id/content"
layout="@layout/content_select_item" />
Next, find it in code and bind it:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_item);
View content= findViewById(R.id.content);
ViewDataBinding binding = DataBindingUtil.bind(content);
Finally, set the variable on the generic binding using generated id inside BR class (like R class but for data binding variables):
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setVariable(BR.item, aSelectItemViewModel.item);
Upvotes: 3