diff options
Diffstat (limited to 'core/java/android/widget/ArrayAdapter.java')
-rw-r--r-- | core/java/android/widget/ArrayAdapter.java | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/core/java/android/widget/ArrayAdapter.java b/core/java/android/widget/ArrayAdapter.java index 97926a7..aff5e29 100644 --- a/core/java/android/widget/ArrayAdapter.java +++ b/core/java/android/widget/ArrayAdapter.java @@ -17,7 +17,9 @@ package android.widget; import android.content.Context; +import android.content.res.Resources; import android.util.Log; +import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -44,7 +46,8 @@ import java.util.List; * or to have some of data besides toString() results fill the views, * override {@link #getView(int, View, ViewGroup)} to return the type of view you want. */ -public class ArrayAdapter<T> extends BaseAdapter implements Filterable { +public class ArrayAdapter<T> extends BaseAdapter implements Filterable, + Spinner.ThemedSpinnerAdapter { /** * Contains the list of objects that represent the data of this ArrayAdapter. * The content of this list is referred to as "the array" in the documentation. @@ -93,6 +96,9 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { private LayoutInflater mInflater; + /** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */ + private LayoutInflater mDropDownInflater; + /** * Constructor * @@ -101,7 +107,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * instantiating views. */ public ArrayAdapter(Context context, int resource) { - init(context, resource, 0, new ArrayList<T>()); + this(context, resource, 0, new ArrayList<T>()); } /** @@ -113,7 +119,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * @param textViewResourceId The id of the TextView within the layout resource to be populated */ public ArrayAdapter(Context context, int resource, int textViewResourceId) { - init(context, resource, textViewResourceId, new ArrayList<T>()); + this(context, resource, textViewResourceId, new ArrayList<T>()); } /** @@ -125,7 +131,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, int resource, T[] objects) { - init(context, resource, 0, Arrays.asList(objects)); + this(context, resource, 0, Arrays.asList(objects)); } /** @@ -138,7 +144,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) { - init(context, resource, textViewResourceId, Arrays.asList(objects)); + this(context, resource, textViewResourceId, Arrays.asList(objects)); } /** @@ -150,7 +156,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, int resource, List<T> objects) { - init(context, resource, 0, objects); + this(context, resource, 0, objects); } /** @@ -163,7 +169,11 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) { - init(context, resource, textViewResourceId, objects); + mContext = context; + mInflater = LayoutInflater.from(context); + mResource = mDropDownResource = resource; + mObjects = objects; + mFieldId = textViewResourceId; } /** @@ -305,14 +315,6 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { mNotifyOnChange = notifyOnChange; } - private void init(Context context, int resource, int textViewResourceId, List<T> objects) { - mContext = context; - mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mResource = mDropDownResource = resource; - mObjects = objects; - mFieldId = textViewResourceId; - } - /** * Returns the context associated with this array adapter. The context is used * to create views from the resource passed to the constructor. @@ -359,16 +361,16 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { * {@inheritDoc} */ public View getView(int position, View convertView, ViewGroup parent) { - return createViewFromResource(position, convertView, parent, mResource); + return createViewFromResource(mInflater, position, convertView, parent, mResource); } - private View createViewFromResource(int position, View convertView, ViewGroup parent, - int resource) { + private View createViewFromResource(LayoutInflater inflater, int position, View convertView, + ViewGroup parent, int resource) { View view; TextView text; if (convertView == null) { - view = mInflater.inflate(resource, parent, false); + view = inflater.inflate(resource, parent, false); } else { view = convertView; } @@ -408,11 +410,40 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable { } /** + * Sets the {@link Resources.Theme} against which drop-down views are + * inflated. + * <p> + * By default, drop-down views are inflated against the theme of the + * {@link Context} passed to the adapter's constructor. + * + * @param theme the theme against which to inflate drop-down views or + * {@code null} to use the theme from the adapter's context + * @see #getDropDownView(int, View, ViewGroup) + */ + @Override + public void setDropDownViewTheme(Resources.Theme theme) { + if (theme == null) { + mDropDownInflater = null; + } else if (theme == mInflater.getContext().getTheme()) { + mDropDownInflater = mInflater; + } else { + final Context context = new ContextThemeWrapper(mContext, theme); + mDropDownInflater = LayoutInflater.from(context); + } + } + + @Override + public Resources.Theme getDropDownViewTheme() { + return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme(); + } + + /** * {@inheritDoc} */ @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { - return createViewFromResource(position, convertView, parent, mDropDownResource); + final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater; + return createViewFromResource(inflater, position, convertView, parent, mDropDownResource); } /** |