package com.agateau.equiv.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.agateau.equiv.R;
import com.agateau.equiv.core.Constants;
import com.agateau.equiv.core.FormatUtils;
import com.agateau.equiv.core.Product;
import com.agateau.equiv.core.ProteinWeightUnit;

import java.util.ArrayList;

/**
 * An adapter to show a list of products
 */
class ProductListAdapter extends ArrayAdapter<Product> {
    private final Kernel mKernel;

    private final CompoundButton.OnCheckedChangeListener mOnFavoriteCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Product product = (Product) buttonView.getTag();
            mKernel.getProductList().setFavorite(product, isChecked);
        }
    };

    public ProductListAdapter(Context context, Kernel kernel, ArrayList<Product> items) {
        super(context, R.layout.product_item, R.id.product_item_text, items);
        mKernel = kernel;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Product product = getItem(position);
        view.setTag(product);

        TextView equivTextView = (TextView) view.findViewById(R.id.product_item_equiv_text);

        float proteins = product.getProteins();

        WeightFormatter formatter = mKernel.getWeightFormater();

        float equiv;
        String equivUnit = product.getUnit();
        int ref;
        if (mKernel.getProteinUnit() == ProteinWeightUnit.POTATO) {
            equiv = 100 / (proteins / Constants.PROTEIN_FOR_POTATO);
            ref = 100;
        } else {
            equiv = 1 / proteins;
            ref = 1;
        }

        String equivText = String.format("%s%s = %d%s",
                FormatUtils.naturalRound(equiv),
                equivUnit,
                ref,
                formatter.getUnitString(WeightFormatter.UnitFormat.FULL));
        equivTextView.setText(equivText);

        CheckBox favoriteCheckBox = (CheckBox) view.findViewById(R.id.product_item_favorite);
        // Reset onCheckedChangeListener so that mOnFavoriteCheckedChangeListener is not called
        // when we call setChecked() (can happen when a view is reused)
        favoriteCheckBox.setOnCheckedChangeListener(null);
        favoriteCheckBox.setChecked(mKernel.getProductList().isFavorite(product));
        favoriteCheckBox.setOnCheckedChangeListener(mOnFavoriteCheckedChangeListener);
        favoriteCheckBox.setTag(product);
        return view;
    }
}

@@ -56,16 +56,52 @@
 ontext;%0A
+import android.view.LayoutInflater;%0A
 import a
@@ -657,16 +657,195 @@
 mKernel;
+%0A    private final Context mContext;%0A%0A    private static class ViewHolder %7B%0A        TextView mainTextView;%0A        TextView equivTextView;%0A        CheckBox favoriteCheckBox;%0A    %7D
 %0A%0A    pr
@@ -1369,38 +1369,42 @@
 em, 
-R.id.product_item_text, items)
+items);%0A        mContext = context
 ;%0A  
@@ -1531,227 +1531,795 @@
 view
- = super.getView(position, convertView, parent);%0A%0A        Product product = getItem(position);%0A        view.setTag(product);%0A%0A        TextView equivTextView = (TextView) view.findViewById(R.id.product_item_equiv_text);%0A
+;%0A        ViewHolder vh;%0A        if (convertView == null) %7B%0A            LayoutInflater inflater = LayoutInflater.from(mContext);%0A            view = inflater.inflate(R.layout.product_item, parent, false);%0A            vh = new ViewHolder();%0A            vh.mainTextView = (TextView) view.findViewById(R.id.product_item_text);%0A            vh.equivTextView = (TextView) view.findViewById(R.id.product_item_equiv_text);%0A            vh.favoriteCheckBox = (CheckBox) view.findViewById(R.id.product_item_favorite);%0A            view.setTag(vh);%0A        %7D else %7B%0A            view = convertView;%0A            vh = (ViewHolder) view.getTag();%0A        %7D%0A%0A        Product product = getItem(position);%0A%0A        // mainTextView%0A        vh.mainTextView.setText(product.getName());%0A%0A        // equivTextView
 %0A   
@@ -2989,24 +2989,27 @@
 ));%0A        
+vh.
 equivTextVie
@@ -3035,32 +3035,26 @@
 );%0A%0A        
-CheckBox
+//
  favoriteChe
@@ -3062,68 +3062,8 @@
 kBox
- = (CheckBox) view.findViewById(R.id.product_item_favorite);
 %0A   
@@ -3227,32 +3227,35 @@
 reused)%0A        
+vh.
 favoriteCheckBox
@@ -3289,32 +3289,35 @@
 (null);%0A        
+vh.
 favoriteCheckBox
@@ -3375,32 +3375,35 @@
 duct));%0A        
+vh.
 favoriteCheckBox
@@ -3473,16 +3473,19 @@
         
+vh.
 favorite

Use a ViewHolder