Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.content.Context;
      4 import android.view.View;
      5 import android.view.ViewGroup;
      6 import android.widget.ArrayAdapter;
      7 import android.widget.Filter;
      8 import android.widget.TextView;
      9 import com.xtremelabs.robolectric.Robolectric;
     10 import com.xtremelabs.robolectric.internal.Implementation;
     11 import com.xtremelabs.robolectric.internal.Implements;
     12 import com.xtremelabs.robolectric.res.ResourceLoader;
     13 
     14 import java.util.ArrayList;
     15 import java.util.Arrays;
     16 import java.util.List;
     17 
     18 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     19 
     20 @SuppressWarnings( { "UnusedDeclaration" })
     21 @Implements(ArrayAdapter.class)
     22 public class ShadowArrayAdapter<T> extends ShadowBaseAdapter {
     23 
     24     private static final Filter STUB_FILTER = new Filter() {
     25         @Override
     26         protected FilterResults performFiltering(CharSequence constraint) {
     27             return null;
     28         }
     29 
     30         @Override
     31         protected void publishResults(CharSequence constraint, FilterResults results) {
     32         }
     33     };
     34 
     35     private Context context;
     36     private List<T> list;
     37     private int resource;
     38     private int textViewResourceId;
     39     private Filter filter;
     40     private boolean notifyOnChange = true;
     41 
     42     public int getTextViewResourceId() {
     43         return textViewResourceId;
     44     }
     45 
     46     public int getResourceId() {
     47         return resource;
     48     }
     49 
     50     public void __constructor__(Context context, int textViewResourceId) {
     51         init(context, textViewResourceId, 0, new ArrayList<T>());
     52     }
     53 
     54     public void __constructor__(Context context, int resource, int textViewResourceId) {
     55         init(context, resource, textViewResourceId, new ArrayList<T>());
     56     }
     57 
     58     public void __constructor__(Context context, int textViewResourceId, T[] objects) {
     59         init(context, textViewResourceId, 0, Arrays.asList(objects));
     60     }
     61 
     62     public void __constructor__(Context context, int resource, int textViewResourceId, T[] objects) {
     63         init(context, resource, textViewResourceId, Arrays.asList(objects));
     64     }
     65 
     66     public void __constructor__(Context context, int textViewResourceId, List<T> objects) {
     67         init(context, textViewResourceId, 0, objects);
     68     }
     69 
     70     public void __constructor__(Context context, int resource, int textViewResourceId, List<T> objects) {
     71         init(context, resource, textViewResourceId, objects);
     72     }
     73 
     74     private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
     75         this.context = context;
     76         this.list = objects;
     77         this.resource = resource;
     78         this.textViewResourceId = textViewResourceId;
     79     }
     80 
     81     @Implementation
     82     public void add(T object) {
     83         list.add(object);
     84         if (notifyOnChange)
     85             notifyDataSetChanged();
     86     }
     87 
     88     @Implementation
     89     public void clear() {
     90         list.clear();
     91         if (notifyOnChange)
     92             notifyDataSetChanged();
     93     }
     94 
     95     @Implementation
     96     public void remove(T object) {
     97         list.remove(object);
     98         if (notifyOnChange)
     99             notifyDataSetChanged();
    100     }
    101 
    102     @Implementation
    103     public void insert(T object, int index) {
    104         list.add(index, object);
    105         if (notifyOnChange)
    106             notifyDataSetChanged();
    107     }
    108 
    109     @Implementation
    110     public Context getContext() {
    111         return context;
    112     }
    113 
    114     @Implementation
    115     public int getCount() {
    116         return list.size();
    117     }
    118 
    119     @Implementation
    120     public T getItem(int position) {
    121         return list.get(position);
    122     }
    123 
    124     @Implementation
    125     public int getPosition(T item) {
    126         return list.indexOf(item);
    127     }
    128 
    129     @Implementation
    130     public View getView(int position, View convertView, ViewGroup parent) {
    131         T item = list.get(position);
    132         View view;
    133 
    134         if (convertView == null) {
    135             view = getResourceLoader().inflateView(context,resource, null);
    136         } else {
    137             view = convertView;
    138         }
    139 
    140         TextView text;
    141         if (textViewResourceId == 0) {
    142             text = (TextView) view;
    143         } else {
    144             text = (TextView) view.findViewById(textViewResourceId);
    145         }
    146 
    147         if (item instanceof CharSequence) {
    148             Robolectric.shadowOf(text).setText((CharSequence)item);
    149         } else {
    150         	Robolectric.shadowOf(text).setText(item.toString());
    151         }
    152 
    153         return view;
    154     }
    155 
    156     @Implementation
    157     public Filter getFilter() {
    158         return STUB_FILTER;
    159     }
    160 
    161     @Override
    162     @Implementation
    163     public void notifyDataSetChanged() {
    164         super.notifyDataSetChanged();
    165         notifyOnChange = true;
    166     }
    167 
    168     @Implementation
    169     public void setNotifyOnChange(boolean notifyOnChange) {
    170         this.notifyOnChange = notifyOnChange;
    171     }
    172 
    173     private ResourceLoader getResourceLoader() {
    174         return shadowOf(Robolectric.application).getResourceLoader();
    175     }
    176 
    177     @Implementation
    178     public static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
    179         CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
    180         return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
    181     }
    182 }