package com.immortal.colzfeed.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
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.immortal.colzfeed.R;
import com.immortal.colzfeed.datamodels.Contact;

public class ContactListAdapter extends ArrayAdapter<Contact> {
	static class ContactHolder {
		private TextView nameField = null;
		private TextView contactField = null;
		private ImageView image = null;

		public ContactHolder(View row) {
			nameField = (TextView) row.findViewById(R.id.name);
			contactField = (TextView) row.findViewById(R.id.contactnumber);
			image = (ImageView) row.findViewById(R.id.contacticon);
		}

		public void populateForm(Contact contact) {
			nameField.setText(contact.getFullName());
			contactField.setText(contact.getContactNo());
			image.setImageResource(R.drawable.ic_launcher);
		}

	}

	Context context;
	int layoutResourceId;
	List<Contact> contactList = new ArrayList<Contact>();

	ContactHolder holder = null;

	public ContactListAdapter(Context context, int textViewResourceId,
			List<Contact> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.layoutResourceId = textViewResourceId;
		this.contactList = objects;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup Parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(R.layout.contact_item, null);
			holder = new ContactHolder(row);
			row.setTag(holder);

		} else {
			holder = (ContactHolder) row.getTag();
		}
		Contact contact = contactList.get(position);
		holder.populateForm(contact);
		return row;
	}

}
