BestPractiseToGo BestPractiseToGo - 1 month ago 7
Android Question

Activity doesn't show RecyclerView - Databinding

I tried databinding with RecyclerView, but my main_activity doesn't show the RecyclerView Content for my Objects. I dont know where the point of failure is.
Thanks for any advice.

Here my simple Code

class: User



public class User {
private String name;
private int age;

public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}


class: UserAdapter



public class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Activity host;
private ArrayList<User> items;

public UserAdapter(Activity activity, ArrayList<User> items) {
this.host = activity;
this.items = items;
Log.i("UserAdapterName", items.get(0).getName().toString()); //log shows Gordon

}

@Override
public UserAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate layout and retrieve binding
ListItemsBinding binding = DataBindingUtil.inflate(host.getLayoutInflater(),
R.layout.list_items, parent, false);;

return new ItemViewHolder(binding);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
User item = items.get(position);

ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
itemViewHolder.bindItem(item);
}

@Override
public int getItemCount() {
return items.size();
}

static class ItemViewHolder extends RecyclerView.ViewHolder {
ListItemsBinding binding;

ItemViewHolder(ListItemsBinding binding) {
super(binding.getRoot());
this.binding = binding;
}

void bindItem(User item) {
binding.setUser(item);
binding.executePendingBindings();
}
}
}


class: MainActivity



public class MainActivity extends AppCompatActivity {
private ArrayList<User> userList = new ArrayList<User>();
private ActivityMainBinding binding;
private UserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
fillData();
adapter = new UserAdapter(MainActivity.this,userList);
binding.recycler.setAdapter(adapter);
}


private void fillData() {
userList.add(new User("Gordon", 26));
userList.add(new User("Dick", 34));
userList.add(new User("Ivan",20));
}
}


layout: activity_main



<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Activity Text"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</RelativeLayout>




layout: list_items



<?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="user"
type="de.bp2go.recgo.User" />

</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
tools:text="Name" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.age}"
android:textColor="@android:color/black"
tools:text="Age" />

</LinearLayout>
</layout>

Answer

For me it looks like You forgot to set proper LayoutManager for RecyclerView.

Missing line for vertical list:

binding.recycler.setLayoutManager(new LinearLayoutManager(this));

see example: RecyclerView

Comments