I did some research I found that Android now support two way databinding. But I am getting error as package not found when I use "@={}". If I use "@{}" it works fine.
Adapter
public class InformationAdapter extends RecyclerView.Adapter<InformationAdapter.DataViewHolder> {
private LayoutInflater inflater;
List<Data> data = Collections.emptyList();
Context context;
public InformationAdapter(List<Data> data, Context context) {
inflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CustomRowBinding binding = CustomRowBinding.inflate(inflater);
DataViewHolder dataViewHolder = new DataViewHolder(binding);
return dataViewHolder;
}
@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
Data current = data.get(position);
holder.cBinding.setUserDetails(current);
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class DataViewHolder extends RecyclerView.ViewHolder {
CustomRowBinding cBinding;
public DataViewHolder(CustomRowBinding binding) {
super(binding.getRoot());
this.cBinding = binding;
}
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="ItemPricing"
type="com.san.viewmodels.Item.ItemPricing" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center_vertical"
android:src="@drawable/coke" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cola Coke"
android:textColor="@color/colorPrimary"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="@+id/item_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{Integer.toString(ItemPricing.unitPrice1)}" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="@+id/item_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{Integer.toString(ItemPricing.unitPrice2)}" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="@+id/item_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Unit"
android:text="@{Integer.toString(ItemPricing.unitPrice3)}" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={Integer.toString(ItemPricing.unitPrice4)}" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</RelativeLayout>
</layout>
If I use "@{something}" this works for me.I think the problem with the =.
POJO Value
public class ItemPricing
{
private int unitPrice1;
public int getUnitPrice1() {
return this.unitPrice1;
}
public void setUnitPrice1(int unitPrice1) {
this.unitPrice1 = unitPrice1;
}
private int unitPrice2;
public int getUnitPrice2() {
return this.unitPrice2;
}
public void setUnitPrice2(int unitPrice2) {
this.unitPrice2 = unitPrice2;
}
private int unitPrice3;
public int getUnitPrice3() {
return this.unitPrice3;
}
public void setUnitPrice3(int unitPrice3) {
this.unitPrice3 = unitPrice3;
}
private int unitPrice4;
public int getUnitPrice4() {
return this.unitPrice4;
}
public void setUnitPrice4(int unitPrice4) {
this.unitPrice4 = unitPrice4;
}
}
Error
I think, you missing two way adapters implementation. Look at my answer for similar problem.