// Copyright 2010 Google Inc. All Rights Reserved.

package com.xtremelabs.robolectric.shadows;

import android.content.Context;
import android.widget.ArrayAdapter;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

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

@SuppressWarnings({"UnusedDeclaration"})
@Implements(ArrayAdapter.class)
public class ShadowArrayAdapter<T> extends ShadowBaseAdapter {

  private Context context;
  private List<T> list;

  public void __constructor__(Context context, int textViewResourceId) {
    init(context, textViewResourceId, 0, new ArrayList<T>());
  }

  public void __constructor__(Context context, int resource, int textViewResourceId) {
    init(context, resource, textViewResourceId, new ArrayList<T>());
  }

  public void __constructor__(Context context, int textViewResourceId, T[] objects) {
    init(context, textViewResourceId, 0, Arrays.asList(objects));
  }

  public void __constructor__(Context context, int resource, int textViewResourceId, T[] objects) {
    init(context, resource, textViewResourceId, Arrays.asList(objects));
  }

  public void __constructor__(Context context, int textViewResourceId, List<T> objects) {
    init(context, textViewResourceId, 0, objects);
  }

  public void __constructor__(Context context, int resource, int textViewResourceId, List<T> objects) {
    init(context, resource, textViewResourceId, objects);
  }

  private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
    this.context = context;
    this.list = objects;
  }

  @Implementation
  public void add(T object) {
    list.add(object);
  }

  @Implementation
  public Context getContext() {
    return context;
  }

  @Implementation
  public int getCount() {
    return list.size();
  }

  @Implementation
  public T getItem(int position) {
    return list.get(position);
  }

  @Implementation
  public int getPosition(T item) {
    return list.indexOf(item);
  }
}

@@ -158,16 +158,73 @@
 dapter;%0A
+import android.view.View;%0Aimport android.view.ViewGroup;%0A
 import c
@@ -328,16 +328,181 @@
 lements;
+%0Aimport com.xtremelabs.robolectric.res.ResourceLoader;%0Aimport com.xtremelabs.robolectric.Robolectric;%0A%0Aimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
 %0A%0Aimport
@@ -759,16 +759,74 @@
 T%3E list;
+%0A  private int resource;%0A  private int textViewResourceId;
 %0A%0A  publ
@@ -901,32 +901,35 @@
    init(context,
+ 0,
  textViewResourc
@@ -932,19 +932,16 @@
 ourceId,
- 0,
  new Arr
@@ -1215,32 +1215,35 @@
    init(context,
+ 0,
  textViewResourc
@@ -1246,19 +1246,16 @@
 ourceId,
- 0,
  Arrays.
@@ -1562,16 +1562,19 @@
 context,
+ 0,
  textVie
@@ -1585,19 +1585,16 @@
 ourceId,
- 0,
  objects
@@ -1915,16 +1915,90 @@
 bjects;%0A
+%09this.resource = resource;%0A%09this.textViewResourceId = textViewResourceId;%0A
   %7D%0A%0A  @
@@ -2393,11 +2393,340 @@
 em);%0A  %7D
+%0A%0A  @Implementation%0A  public View getView(int position, View convertView, ViewGroup parent) %7B%0A    T item = list.get(position);%0A    return getResourceLoader().inflateView(context, textViewResourceId, parent);%0A  %7D%0A%0A  private ResourceLoader getResourceLoader() %7B%0A    return shadowOf(Robolectric.application).getResourceLoader();%0A  %7D
 %0A%7D%0A

Implement getView for ShadowArrayAdapter