package net.cattaka.android.snippets.adapter;

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

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

/**
 * Created by cattaka on 16/05/01.
 */
public class CodeLabelAdapter extends ArrayAdapter<ICodeLabel> {
    @LayoutRes
    private int mResource;
    @LayoutRes
    private int mDropDownResource;

    public static CodeLabelAdapter newInstance(Context context, ICodeLabel[] objects, boolean withNullValue) {
        if (withNullValue) {
            List<ICodeLabel> values = new ArrayList<>();
            values.add(null);
            values.addAll(Arrays.asList(objects));
            return new CodeLabelAdapter(context, android.R.layout.simple_list_item_1, android.R.layout.simple_spinner_dropdown_item, values);
        } else {
            return new CodeLabelAdapter(context, android.R.layout.simple_list_item_1, android.R.layout.simple_spinner_dropdown_item, objects);
        }
    }

    public static CodeLabelAdapter newInstance(Context context, List<ICodeLabel> objects, boolean withNullValue) {
        if (withNullValue) {
            List<ICodeLabel> values = new ArrayList<>();
            values.add(null);
            values.addAll(objects);
            return new CodeLabelAdapter(context, android.R.layout.simple_list_item_1, android.R.layout.simple_spinner_dropdown_item, values);
        } else {
            return new CodeLabelAdapter(context, android.R.layout.simple_list_item_1, android.R.layout.simple_spinner_dropdown_item, objects);
        }
    }

    public CodeLabelAdapter(Context context, @LayoutRes int resource, @LayoutRes int dropDownResource, ICodeLabel[] objects) {
        super(context, resource, objects);
        mResource = resource;
        mDropDownResource = dropDownResource;
    }

    public CodeLabelAdapter(Context context, @LayoutRes int resource, @LayoutRes int dropDownResource, List<ICodeLabel> objects) {
        super(context, resource, objects);
        mResource = resource;
        mDropDownResource = dropDownResource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view;
        if (convertView == null) {
            view = (TextView) LayoutInflater.from(getContext()).inflate(mResource, null);
        } else {
            view = (TextView) convertView;
        }
        ICodeLabel item = getItem(position);
        if (item != null) {
            view.setText(item.getLabel(getContext().getResources()));
        } else {
            view.setText("");
        }
        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView view;
        if (convertView == null) {
            view = (TextView) LayoutInflater.from(getContext()).inflate(mDropDownResource, parent, false);
        } else {
            view = (TextView) convertView;
        }
        ICodeLabel item = getItem(position);
        if (item != null) {
            view.setText(item.getLabel(getContext().getResources()));
        } else {
            view.setText("");
        }
        return view;
    }
}

@@ -2340,39 +2340,16 @@
 rent) %7B%0A
-        TextView view;%0A
         
@@ -2387,33 +2387,29 @@
         
-view = (Tex
+conver
 tView
-)
+ =
  LayoutI
@@ -2462,39 +2462,32 @@
 null);%0A        %7D
- else %7B
 %0A            vie
@@ -2471,35 +2471,40 @@
       %7D%0A        
-   
+TextView
  view = (TextVie
@@ -2513,35 +2513,58 @@
  convertView
-;%0A        %7D
+.findViewById(android.R.id.text1);
 %0A        ICo
@@ -2882,31 +2882,8 @@
 ) %7B%0A
-        TextView view;%0A
     
@@ -2925,33 +2925,29 @@
         
-view = (Tex
+conver
 tView
-)
+ =
  LayoutI
@@ -3004,21 +3004,12 @@
 ce, 
-parent, false
+null
 );%0A 
@@ -3008,39 +3008,32 @@
 null);%0A        %7D
- else %7B
 %0A            vie
@@ -3021,27 +3021,32 @@
   %7D%0A        
-   
+TextView
  view = (Tex
@@ -3063,27 +3063,50 @@
 vertView
-;%0A        %7D
+.findViewById(android.R.id.text1);
 %0A       

Update CodeLabelAdapter