package xyz.igorgee.imagecreator3d;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

import xyz.igorgee.imagecreatorg3dx.ObjectViewer;
import xyz.igorgee.utilities.UIUtilities;

public class CustomAdapter extends ArrayAdapter {
    private final Context context;
    private final ArrayList<String> items;

    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.items = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        final TextView textView = (TextView) rowView.findViewById(R.id.image_name);
        Button buy = (Button) rowView.findViewById(R.id.button_buy);
        Button view3d = (Button) rowView.findViewById(R.id.button_3d_view);

        textView.setText(items.get(position));

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtilities.makeSnackbar(v, "Added to Cart!");
            }
        });

        view3d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fileName = (textView).getText().toString();
                File model = new File(HomePageFragment.modelsDirectory + "/" + fileName + "/test.g3db");
                Intent viewModel = new Intent(context, ObjectViewer.class);
                viewModel.putExtra("model", model);
                context.startActivity(viewModel);
            }
        });

        return rowView;
    }
}

@@ -252,16 +252,49 @@
 Button;%0A
+import android.widget.ImageView;%0A
 import a
@@ -368,16 +368,101 @@
 yList;%0A%0A
+import butterknife.Bind;%0Aimport butterknife.ButterKnife;%0Aimport butterknife.OnClick;%0A
 import x
@@ -675,16 +675,932 @@
 items;%0A%0A
+    private LayoutInflater inflater;%0A%0A    class ViewHolder %7B%0A        @Bind(R.id.image) ImageView image;%0A        @Bind(R.id.image_name) TextView textView;%0A        @Bind(R.id.button_buy) Button buy;%0A        @Bind(R.id.button_3d_view) Button view3d;%0A%0A        public ViewHolder(View view) %7B%0A            ButterKnife.bind(this, view);%0A        %7D%0A%0A        @OnClick(R.id.button_buy)%0A        public void buyModel(View view) %7B%0A            UIUtilities.makeSnackbar(view, %22Added to Cart!%22);%0A        %7D%0A%0A        @OnClick(R.id.button_3d_view)%0A        public void viewIn3D(View view) %7B%0A            String fileName = (textView).getText().toString();%0A            File model = new File(HomePageFragment.modelsDirectory + %22/%22 + fileName + %22/test.g3db%22);%0A            Intent viewModel = new Intent(context, ObjectViewer.class);%0A            viewModel.putExtra(%22model%22, model);%0A            context.startActivity(viewModel);%0A        %7D%0A    %7D%0A%0A
     publ
@@ -1893,16 +1893,9 @@
 iew 
-convertV
+v
 iew,
@@ -1914,16 +1914,48 @@
 arent) %7B
+%0A%0A        ViewHolder viewHolder;
 %0A       
@@ -2074,24 +2074,25 @@
 RVICE);%0A
+%0A
         
 View row
@@ -2087,17 +2087,41 @@
     
-View rowV
+if (view == null) %7B%0A            v
 iew 
@@ -2181,238 +2181,55 @@
     
-final TextView textView = (TextView) rowView.findViewById(R.id.image_name);%0A        Button buy = (Button) rowView.findViewById(R.id.button_buy);%0A        Button view3d = (Button) rowView.findViewById(R.id.button_3d_
+    viewHolder = new ViewHolder(
 view);%0A
-%0A
         
 text
@@ -2228,558 +2228,104 @@
     
-textView.setText(items.get(position));%0A%0A        buy.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                UIUtilities.makeSnackbar(v, %22Added to Cart!%22);%0A            %7D%0A        %7D);%0A%0A        view3d.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                String fileName = (textView).getText().toString();%0A                File model = new File(HomePageFragment.modelsDirectory + %22/%22 + fileName + %22/test.g3db%22
+    view.setTag(viewHolder);%0A        %7D else %7B%0A            viewHolder = (ViewHolder) view.getTag(
 );%0A 
@@ -2327,32 +2327,35 @@
 ();%0A        
+%7D%0A%0A
         
 Intent viewM
@@ -2346,193 +2346,55 @@
     
-Intent viewModel = new Intent(context, ObjectViewer.class);%0A                viewModel.putExtra(%22model%22, model);%0A                context.startActivity(viewModel);%0A            %7D%0A        %7D
+viewHolder.textView.setText(items.get(position)
 );%0A%0A
@@ -2412,12 +2412,9 @@
 urn 
-rowV
+v
 iew;

Implement ViewHolder Pattern