Layout error on Edit text on Two way data binding android

1.1k views Asked by At

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

enter image description here

2

There are 2 answers

0
Sergei Bubenshchikov On

I think, you missing two way adapters implementation. Look at my answer for similar problem.

2
Lovekesh On
package com.comfixme.fixer.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.UiThread;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.comfixme.fixer.Model.CategoryModel;
import com.comfixme.fixer.R;
import com.comfixme.fixer.databinding.RowCategoryListBinding;

import java.util.List;


/**
 * Created by Argalon-PC on 8/15/2016.
 */
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    private final Context mContext;
    private List<CategoryModel> categoryModelArrayList;
    private OnItemClickListener onItemClickListener;

    String TAG = "CategoryAdapter";


    public CategoryAdapter(Context mContext, List<CategoryModel> categoryModelArrayList) {

        this.categoryModelArrayList = categoryModelArrayList;
        this.mContext = mContext;

    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        final RowCategoryListBinding binding = DataBindingUtil.inflate(inflater, R.layout.row_category_list, parent, false);
        return new ViewHolder(binding.getRoot(), binding);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CategoryModel categoryModel = this.categoryModelArrayList.get(position);
        holder.bind(categoryModel);
    }

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

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    public List<CategoryModel> getCategory() {
        return categoryModelArrayList;
    }

    public void setCategoryModelArrayList(List<CategoryModel> categoryModelArrayList) {
        this.categoryModelArrayList = categoryModelArrayList;
        notifyDataSetChanged();
    }

    private void dataSetChanged() {
        this.notifyDataSetChanged();

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;


    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private final RowCategoryListBinding binding;




        public ViewHolder(final View view, final RowCategoryListBinding binding) {
            super(view);
            this.binding = binding;
            view.setOnClickListener(this);
            binding.mainLinear.setOnClickListener(this);
            binding.cardView.setOnClickListener(this);
            binding.imageViewSubcate.setOnClickListener(this);
        }



        @Override
        public void onClick(View v) {
            try {

                Log.e(TAG, "onClick() called with: v = [" + onItemClickListener + "]");
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, getAdapterPosition());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @UiThread
        public void bind(final CategoryModel categoryModel) {
            this.binding.setCategory(categoryModel);
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);



    }
}