ShadowArrayAdapter.java revision f2bc42e35b96307f81f8069ada91be57be95e43b
1a775c2f6886281de846d631cabc1a763a6dabe24mylaccpackage com.xtremelabs.robolectric.shadows; 2a775c2f6886281de846d631cabc1a763a6dabe24mylacc 310f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vrankenimport static com.xtremelabs.robolectric.Robolectric.shadowOf; 410f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 510f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vrankenimport java.util.ArrayList; 610f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vrankenimport java.util.Arrays; 710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vrankenimport java.util.List; 810f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 9a775c2f6886281de846d631cabc1a763a6dabe24mylaccimport android.content.Context; 10853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport android.view.View; 11853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport android.view.ViewGroup; 12a775c2f6886281de846d631cabc1a763a6dabe24mylaccimport android.widget.ArrayAdapter; 133783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirshimport android.widget.Filter; 14e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richardimport android.widget.TextView; 1510f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 16f540e3687a4ca75ea6b40f577b7807cca30119bePhil Goodwinimport com.xtremelabs.robolectric.Robolectric; 17c2e21b4055efb3648d243cfdd871f0dbc1db4046Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implementation; 18c2e21b4055efb3648d243cfdd871f0dbc1db4046Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implements; 19853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport com.xtremelabs.robolectric.res.ResourceLoader; 20a775c2f6886281de846d631cabc1a763a6dabe24mylacc 214baf90fcda4a6d4c5cad3e8f857be1f4a070074eRonald Dehuysser@SuppressWarnings( { "UnusedDeclaration" }) 22a775c2f6886281de846d631cabc1a763a6dabe24mylacc@Implements(ArrayAdapter.class) 23a775c2f6886281de846d631cabc1a763a6dabe24mylaccpublic class ShadowArrayAdapter<T> extends ShadowBaseAdapter { 24a775c2f6886281de846d631cabc1a763a6dabe24mylacc 253783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh private static final Filter STUB_FILTER = new Filter() { 263783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh @Override 273783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh protected FilterResults performFiltering(CharSequence constraint) { 283783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh return null; 293783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh } 303783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh 313783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh @Override 323783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh protected void publishResults(CharSequence constraint, FilterResults results) { 333783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh } 343783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh }; 353783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh 3672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private Context context; 3772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private List<T> list; 3872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private int resource; 3972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private int textViewResourceId; 403783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh private Filter filter; 4172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 4210f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken public int getTextViewResourceId() { 4310f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken return textViewResourceId; 4410f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken } 4510f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 4610f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken public int getResourceId() { 4710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken return resource; 4810f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken } 4910f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 5072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int textViewResourceId) { 5110f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken init(context, textViewResourceId, 0, new ArrayList<T>()); 5272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 5372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 5472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int resource, int textViewResourceId) { 5572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin init(context, resource, textViewResourceId, new ArrayList<T>()); 5672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 5772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 5872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int textViewResourceId, T[] objects) { 5910f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken init(context, textViewResourceId, 0, Arrays.asList(objects)); 6072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 6172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 6272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int resource, int textViewResourceId, T[] objects) { 6372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin init(context, resource, textViewResourceId, Arrays.asList(objects)); 6472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 6572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 6672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int textViewResourceId, List<T> objects) { 6710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken init(context, textViewResourceId, 0, objects); 6872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 6972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 7072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void __constructor__(Context context, int resource, int textViewResourceId, List<T> objects) { 7172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin init(context, resource, textViewResourceId, objects); 7272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 7372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 7472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private void init(Context context, int resource, int textViewResourceId, List<T> objects) { 7572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin this.context = context; 7672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin this.list = objects; 7772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin this.resource = resource; 7872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin this.textViewResourceId = textViewResourceId; 7972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 8072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 8172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 8272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public void add(T object) { 8372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin list.add(object); 8472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 8572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 8672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 87a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard public void remove(T object) { 88a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard list.remove(object); 89a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard } 90a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard 91a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard @Implementation 9206cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore public void insert(T object, int index) { 9306cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore list.add(index, object); 9406cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore } 9506cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore 9606cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore @Implementation 9772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public Context getContext() { 9872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return context; 9972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 10072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 10172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 10272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public int getCount() { 10372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return list.size(); 10472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 10572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 10672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 10772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public T getItem(int position) { 10872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return list.get(position); 10972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 11072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 11172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 11272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public int getPosition(T item) { 11372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return list.indexOf(item); 11472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 11572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 11672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 11772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public View getView(int position, View convertView, ViewGroup parent) { 11872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin T item = list.get(position); 119e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard View view = getResourceLoader().inflateView(context, resource, parent); 120e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard 12110f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken if (convertView == null) { 12210f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken view = getResourceLoader().inflateView(context,resource, parent); 12310f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken } else { 12410f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken view = convertView; 12510f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken } 12610f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken 127e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard TextView text; 128e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard if (textViewResourceId == 0) { 129e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard text = (TextView) view; 130e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard } else { 131e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard text = (TextView) view.findViewById(textViewResourceId); 132e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard } 133e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard 134e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard if (item instanceof CharSequence) { 135f2bc42e35b96307f81f8069ada91be57be95e43bMichael Portuesi Robolectric.shadowOf(text).setText((CharSequence)item); 136e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard } else { 137f2bc42e35b96307f81f8069ada91be57be95e43bMichael Portuesi Robolectric.shadowOf(text).setText(item.toString()); 138e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard } 139e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard 140e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard return view; 14172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 14272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 1433783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh @Implementation 1443783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh public Filter getFilter() { 1453783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh return STUB_FILTER; 1463783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh } 1473783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh 14872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin private ResourceLoader getResourceLoader() { 14972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return shadowOf(Robolectric.application).getResourceLoader(); 15072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 15172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin 15272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin @Implementation 15372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin public static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) { 15472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin CharSequence[] strings = context.getResources().getTextArray(textArrayResId); 15572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin return new ArrayAdapter<CharSequence>(context, textViewResId, strings); 15672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin } 1574baf90fcda4a6d4c5cad3e8f857be1f4a070074eRonald Dehuysser}