package com.javacodegeeks.android.example.androidmultitouchlist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyArrayAdapter extends ArrayAdapter<Student> {

	Context context;
	int layoutResourceId;
	ArrayList<Student> students = new ArrayList<Student>();

	public MyArrayAdapter(Context context, int layoutResourceId,
			ArrayList<Student> studs) {
		super(context, layoutResourceId, studs);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.students = studs;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = convertView;
		StudentWrapper StudentWrapper = null;

		if (item == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			item = inflater.inflate(layoutResourceId, parent, false);
			StudentWrapper = new StudentWrapper();
			StudentWrapper.name = (TextView) item.findViewById(R.id.textName);
			StudentWrapper.age = (TextView) item.findViewById(R.id.textAge);
			StudentWrapper.address = (TextView) item.findViewById(R.id.textAddr);
			StudentWrapper.edit = (Button) item.findViewById(R.id.btnEdit);
			StudentWrapper.delete = (Button) item.findViewById(R.id.btnDelete);
			item.setTag(StudentWrapper);
		} else {
			StudentWrapper = (StudentWrapper) item.getTag();
		}

		Student student = students.get(position);
		StudentWrapper.name.setText(student.getName());
		StudentWrapper.age.setText(student.getAge());
		StudentWrapper.address.setText(student.getAddress());
		
		StudentWrapper.edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(context, "Edit", Toast.LENGTH_LONG).show();
			}
		});
		
		StudentWrapper.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(context, "Delete", Toast.LENGTH_LONG).show();
			}
		});

		return item;

	}

	static class StudentWrapper {
		TextView name;
		TextView age;
		TextView address;
		Button edit;
		Button delete;
	}

}
