Reputation: 11071
I've made a simple binding in RecyclerView.
I created ViewHolder like this:
class ItemViewHolder extends RecyclerView.ViewHolder{
ItemBinding binding;
public ItemViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
}
Binding is done like this
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
MyItem item= items.get(position);
// bind model with data
holder.binding.setItem(item);
}
I read this tutorial and see that ViewHolder is like this:
public class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private final ViewDataBinding binding;
public MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj) {
binding.setVariable(BR.obj,obj);
binding.executePendingBindings();
}
}
And Adapter is
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final TemperatureData temperatureData = data.get(position);
holder.bind(temperatureData);
}
My question is why do we need public void bind(Object obj)
method if it works without it?
I have less code and it works but being new to Android I'm scared to miss something.
Upvotes: 0
Views: 88
Reputation: 3348
This method ensures setters for any class will be generated.
In most cases though, it is not necesssary.
So yes, you can delete this method - worst thing that can happen here is that you add a class to your app for which the setter wouldn't generate (at which point you know where to look for a solution)
Upvotes: 1