package com.trydroid.multiplelayoutlistview;

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

import java.util.List;

public class MultipleLayoutAdapter extends BaseAdapter {

    private Context    context;
    private List<Item> items;

    public MultipleLayoutAdapter(Context context, List<Item> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Item getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Item item = getItem(position);
        int viewType = getItemViewType(position);
        switch (viewType) {
            case Item.TYPE_EDITTEXT:
                convertView = inflateEditTextView(convertView, item);
                break;

            case Item.TYPE_TEXTVIEW:
                convertView = inflateTextView(convertView, item);
                break;

            case Item.TYPE_IMAGEVIEW:
                convertView = inflateImageView(convertView, item);
                break;
        }
        return convertView;
    }

    private View inflateEditTextView(View convertView, Item item) {
        EditTextViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.edittext_list_row, null);
            viewHolder = new EditTextViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (EditTextViewHolder) convertView.getTag();
        }

        viewHolder.editText.setText(item.getData());

        return convertView;
    }

    private View inflateTextView(View convertView, Item item) {
        TextViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.textview_list_row, null);
            viewHolder = new TextViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (TextViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(item.getData());

        return convertView;
    }

    private View inflateImageView(View convertView, Item item) {
        ImageViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.imageview_list_row, null);
            viewHolder = new ImageViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ImageViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(item.getData());

        return convertView;
    }


    static class EditTextViewHolder {
        private EditText editText;

        public EditTextViewHolder(View convertView) {
            this.editText = (EditText) convertView.findViewById(R.id.edittext);
        }
    }

    static class TextViewHolder {
        private TextView textView;

        public TextViewHolder(View convertView) {
            this.textView = (TextView) convertView.findViewById(R.id.textview);
        }
    }

    static class ImageViewHolder {
        private ImageView imageView;
        private TextView  textView;

        public ImageViewHolder(View convertView) {
            this.imageView = (ImageView) convertView.findViewById(R.id.imageview);
            this.textView = (TextView) convertView.findViewById(R.id.textview);
        }
    }

}

@@ -1313,32 +1313,40 @@
 iew(convertView,
+ parent,
  item);%0A        
@@ -1448,32 +1448,40 @@
 iew(convertView,
+ parent,
  item);%0A        
@@ -1593,16 +1593,24 @@
 ertView,
+ parent,
  item);%0A
@@ -1723,32 +1723,50 @@
 iew convertView,
+ ViewGroup parent,
  Item item) %7B%0A  
@@ -1920,36 +1920,45 @@
 ttext_list_row, 
-null
+parent, false
 );%0A            v
@@ -2285,32 +2285,50 @@
 iew convertView,
+ ViewGroup parent,
  Item item) %7B%0A  
@@ -2478,36 +2478,45 @@
 tview_list_row, 
-null
+parent, false
 );%0A            v
@@ -2844,16 +2844,34 @@
 ertView,
+ ViewGroup parent,
  Item it
@@ -3039,20 +3039,29 @@
 st_row, 
-null
+parent, false
 );%0A     

add parent argument to LayoutInflater