package com.codepath.apps.simpletweets.adapters;

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

import com.codepath.apps.simpletweets.R;
import com.codepath.apps.simpletweets.models.Tweet;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TweetsArrayAdapter extends ArrayAdapter<Tweet> {

    public TweetsArrayAdapter(Context context, List<Tweet> tweets) {
        super(context, 0, tweets);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Tweet tweet = getItem(position);

        if(convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_tweet, parent, false);
        }

        ImageView ivProfileImage = (ImageView) convertView.findViewById(R.id.ivProfile);
        TextView tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
        TextView tvBody = (TextView) convertView.findViewById(R.id.tvTweetBody);

        tvUserName.setText(tweet.getUser().getScreenName());
        tvBody.setText(tweet.getBody());
        ivProfileImage.setImageResource(android.R.color.transparent); // clear out the old image for a recycled view
        Picasso.with(getContext()).load(tweet.getUser().getProfileImageUrl()).into(ivProfileImage);

        return convertView;
    }
}
@@ -743,33 +743,137 @@
     
-if(convertView == null) %7B
+ViewHolder viewHolder; // lookup cached view in tag%0A%0A        if(convertView == null) %7B%0A            viewHolder = new ViewHolder();
 %0A   
@@ -986,52 +986,41 @@
     
-%7D%0A%0A
     
-    ImageView ivProfileImage = (Image
+viewHolder.tvUserName = (Text
 View
@@ -1051,24 +1051,25 @@
 Id(R.id.
-ivProfil
+tvUserNam
 e);%0A    
@@ -1076,27 +1076,29 @@
     
-TextView tvUserName
+    viewHolder.tvBody
  = (
@@ -1135,32 +1135,33 @@
 ById(R.id.tv
-UserName
+TweetBody
 );%0A        T
@@ -1163,90 +1163,246 @@
     
-TextView tvBody = (TextView) convertView.findViewById(R.id.tvTweetBody);%0A%0A        
+    viewHolder.ivProfileImage = (ImageView) convertView.findViewById(R.id.ivProfile);%0A%0A            convertView.setTag(viewHolder);%0A        %7D else %7B%0A            viewHolder = (ViewHolder) convertView.getTag();%0A        %7D%0A%0A        viewHolder.
 tvUs
@@ -1458,16 +1458,27 @@
         
+viewHolder.
 tvBody.s
@@ -1510,16 +1510,27 @@
         
+viewHolder.
 ivProfil
@@ -1713,16 +1713,27 @@
 )).into(
+viewHolder.
 ivProfil
@@ -1775,10 +1775,143 @@
 w;%0A    %7D
+%0A%0A    private static class ViewHolder %7B%0A        TextView tvUserName;%0A        TextView tvBody;%0A        ImageView ivProfileImage;%0A    %7D
 %0A%7D

add the viewHolder pattern to the TweetsArrayAdapter