package com.nbusy.app;

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

import java.util.List;

public class MessageListArrayAdapter extends ArrayAdapter<Messages.Message> {
    private final Context context;
    private final List<Messages.Message> values;

    public MessageListArrayAdapter(Context context, List<Messages.Message> values) {
        super(context, R.layout.message_list_row, values);
        this.context = context;
        this.values = values;
    }

    @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.message_list_row, parent, false);
        getItem(position);
        Messages.Message contact = values.get(position);

        TextView contactName = (TextView) rowView.findViewById(R.id.contact_name);
        contactName.setText(contact.name);

        TextView lastMessage = (TextView) rowView.findViewById(R.id.last_message);
        lastMessage.setText(contact.message);

        TextView sent = (TextView) rowView.findViewById(R.id.sent);
        sent.setText(contact.sent);

        // change the icon for Windows and iPhone
//        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
//        String s = objects[position];
//        if (s.startsWith("iPhone")) {
//            imageView.setImageResource(R.drawable.no);
//        } else {
//            imageView.setImageResource(R.drawable.ok);
//        }

        return rowView;
    }

    // view holder pattern template
    static class ViewHolder {
        TextView contactName;
        TextView lastMessage;
        TextView sent;
    }
}

@@ -316,92 +316,8 @@
 e%3E %7B
-%0A    private final Context context;%0A    private final List%3CMessages.Message%3E values;
 %0A%0A  
@@ -462,70 +462,8 @@
 s);%0A
-        this.context = context;%0A        this.values = values;%0A
     
@@ -567,295 +567,462 @@
     
-LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);%0A        View rowView = inflater.inflate(R.layout.message_list_row, parent, false);%0A        getItem(position);%0A        Messages.Message contact = values.get(position);%0A%0A        TextView 
+Messages.Message message = getItem(position);%0A%0A        // check if an existing view is being reused, otherwise inflate the view%0A        ViewHolder viewHolder; // view lookup cache stored in tag%0A        if (convertView == null) %7B%0A            viewHolder = new ViewHolder();%0A            LayoutInflater inflater = LayoutInflater.from(getContext());%0A            convertView = inflater.inflate(R.layout.message_list_row, parent, false);%0A            viewHolder.
 cont
@@ -1034,35 +1034,39 @@
 me = (TextView) 
-row
+convert
 View.findViewByI
@@ -1099,61 +1099,23 @@
     
-contactName.setText(contact.name);%0A%0A        TextView 
+    viewHolder.
 last
@@ -1127,35 +1127,39 @@
 ge = (TextView) 
-row
+convert
 View.findViewByI
@@ -1192,123 +1192,340 @@
     
-lastMessage.setText(contact.message);%0A%0A        TextView sent = (TextView) rowView.findViewById(R.id.sent);%0A        
+    viewHolder.sent = (TextView) convertView.findViewById(R.id.sent);%0A            convertView.setTag(viewHolder);%0A        %7D else %7B%0A            viewHolder = (ViewHolder) convertView.getTag();%0A        %7D%0A%0A        viewHolder.contactName.setText(message.name);%0A        viewHolder.lastMessage.setText(message.message);%0A        viewHolder.
 sent
@@ -1533,23 +1533,23 @@
 setText(
-contact
+message
 .sent);%0A
@@ -1917,19 +1917,23 @@
  return 
-row
+convert
 View;%0A  

use viewholder pattern