The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.ArrayRes; |
| 20 | import android.annotation.IdRes; |
| 21 | import android.annotation.LayoutRes; |
Tor Norbye | 417ee5b | 2015-03-10 20:57:37 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 23 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 25 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.util.Log; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 27 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Arrays; |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 34 | import java.util.Collection; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 35 | import java.util.Collections; |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 36 | import java.util.Comparator; |
| 37 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Joe Fernandez | d7ef95a | 2017-04-24 13:50:13 -0700 | [diff] [blame] | 40 | * You can use this adapter to provide views for an {@link AdapterView}, |
| 41 | * Returns a view for each object in a collection of data objects you |
| 42 | * provide, and can be used with list-based user interface widgets such as |
| 43 | * {@link ListView} or {@link Spinner}. |
| 44 | * <p> |
| 45 | * By default, the array adapter creates a view by calling {@link Object#toString()} on each |
| 46 | * data object in the collection you provide, and places the result in a TextView. |
| 47 | * You may also customize what type of view is used for the data object in the collection. |
| 48 | * To customize what type of view is used for the data object, |
| 49 | * override {@link #getView(int, View, ViewGroup)} |
| 50 | * and inflate a view resource. |
| 51 | * For a code example, see |
| 52 | * the <a href="https://developer.android.com/samples/CustomChoiceList/index.html"> |
| 53 | * CustomChoiceList</a> sample. |
| 54 | * </p> |
| 55 | * <p> |
| 56 | * For an example of using an array adapter with a ListView, see the |
| 57 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html#AdapterViews"> |
| 58 | * Adapter Views</a> guide. |
| 59 | * </p> |
| 60 | * <p> |
| 61 | * For an example of using an array adapter with a Spinner, see the |
| 62 | * <a href="{@docRoot}guide/topics/ui/controls/spinner.html">Spinners</a> guide. |
| 63 | * </p> |
| 64 | * <p class="note"><strong>Note:</strong> |
| 65 | * If you are considering using array adapter with a ListView, consider using |
| 66 | * {@link android.support.v7.widget.RecyclerView} instead. |
| 67 | * RecyclerView offers similar features with better performance and more flexibility than |
| 68 | * ListView provides. |
| 69 | * See the |
| 70 | * <a href="https://developer.android.com/guide/topics/ui/layout/recyclerview.html"> |
| 71 | * Recycler View</a> guide.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | */ |
Alan Viverette | 2add9bc | 2015-06-02 14:54:40 -0700 | [diff] [blame] | 73 | public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * Lock used to modify the content of {@link #mObjects}. Any write operation |
| 76 | * performed on the array should be synchronized on this lock. This lock is also |
| 77 | * used by the filter (see {@link #getFilter()} to make a synchronized copy of |
| 78 | * the original array of data. |
| 79 | */ |
| 80 | private final Object mLock = new Object(); |
| 81 | |
Alan Viverette | 7d5967e | 2015-06-03 16:27:36 -0700 | [diff] [blame] | 82 | private final LayoutInflater mInflater; |
| 83 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 84 | private final Context mContext; |
Alan Viverette | 7d5967e | 2015-06-03 16:27:36 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | /** |
| 87 | * The resource indicating what views to inflate to display the content of this |
| 88 | * array adapter. |
| 89 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 90 | private final int mResource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * The resource indicating what views to inflate to display the content of this |
| 94 | * array adapter in a drop down widget. |
| 95 | */ |
| 96 | private int mDropDownResource; |
| 97 | |
| 98 | /** |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 99 | * Contains the list of objects that represent the data of this ArrayAdapter. |
| 100 | * The content of this list is referred to as "the array" in the documentation. |
| 101 | */ |
| 102 | private List<T> mObjects; |
| 103 | |
| 104 | /** |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 105 | * Indicates whether the contents of {@link #mObjects} came from static resources. |
| 106 | */ |
| 107 | private boolean mObjectsFromResources; |
| 108 | |
| 109 | /** |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 110 | * If the inflated resource is not a TextView, {@code mFieldId} is used to find |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * a TextView inside the inflated views hierarchy. This field must contain the |
| 112 | * identifier that matches the one defined in the resource file. |
| 113 | */ |
| 114 | private int mFieldId = 0; |
| 115 | |
| 116 | /** |
| 117 | * Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever |
| 118 | * {@link #mObjects} is modified. |
| 119 | */ |
| 120 | private boolean mNotifyOnChange = true; |
| 121 | |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 122 | // A copy of the original mObjects array, initialized from and then used instead as soon as |
| 123 | // the mFilter ArrayFilter is used. mObjects will then only contain the filtered values. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private ArrayList<T> mOriginalValues; |
| 125 | private ArrayFilter mFilter; |
| 126 | |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 127 | /** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */ |
| 128 | private LayoutInflater mDropDownInflater; |
| 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Constructor |
| 132 | * |
| 133 | * @param context The current context. |
Romain Guy | 9c5d1b1 | 2013-01-09 20:16:37 -0800 | [diff] [blame] | 134 | * @param resource The resource ID for a layout file containing a TextView to use when |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | * instantiating views. |
| 136 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 137 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) { |
| 138 | this(context, resource, 0, new ArrayList<>()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Constructor |
| 143 | * |
| 144 | * @param context The current context. |
| 145 | * @param resource The resource ID for a layout file containing a layout to use when |
| 146 | * instantiating views. |
| 147 | * @param textViewResourceId The id of the TextView within the layout resource to be populated |
| 148 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 149 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 150 | @IdRes int textViewResourceId) { |
| 151 | this(context, resource, textViewResourceId, new ArrayList<>()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Constructor |
| 156 | * |
| 157 | * @param context The current context. |
Romain Guy | 9c5d1b1 | 2013-01-09 20:16:37 -0800 | [diff] [blame] | 158 | * @param resource The resource ID for a layout file containing a TextView to use when |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | * instantiating views. |
| 160 | * @param objects The objects to represent in the ListView. |
| 161 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 162 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 163 | this(context, resource, 0, Arrays.asList(objects)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Constructor |
| 168 | * |
| 169 | * @param context The current context. |
| 170 | * @param resource The resource ID for a layout file containing a layout to use when |
| 171 | * instantiating views. |
| 172 | * @param textViewResourceId The id of the TextView within the layout resource to be populated |
| 173 | * @param objects The objects to represent in the ListView. |
| 174 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 175 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 176 | @IdRes int textViewResourceId, @NonNull T[] objects) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 177 | this(context, resource, textViewResourceId, Arrays.asList(objects)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Constructor |
| 182 | * |
| 183 | * @param context The current context. |
Romain Guy | 9c5d1b1 | 2013-01-09 20:16:37 -0800 | [diff] [blame] | 184 | * @param resource The resource ID for a layout file containing a TextView to use when |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | * instantiating views. |
| 186 | * @param objects The objects to represent in the ListView. |
| 187 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 188 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 189 | @NonNull List<T> objects) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 190 | this(context, resource, 0, objects); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Constructor |
| 195 | * |
| 196 | * @param context The current context. |
| 197 | * @param resource The resource ID for a layout file containing a layout to use when |
| 198 | * instantiating views. |
| 199 | * @param textViewResourceId The id of the TextView within the layout resource to be populated |
| 200 | * @param objects The objects to represent in the ListView. |
| 201 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 202 | public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 203 | @IdRes int textViewResourceId, @NonNull List<T> objects) { |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 204 | this(context, resource, textViewResourceId, objects, false); |
| 205 | } |
| 206 | |
| 207 | private ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 208 | @IdRes int textViewResourceId, @NonNull List<T> objects, boolean objsFromResources) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 209 | mContext = context; |
| 210 | mInflater = LayoutInflater.from(context); |
| 211 | mResource = mDropDownResource = resource; |
| 212 | mObjects = objects; |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 213 | mObjectsFromResources = objsFromResources; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 214 | mFieldId = textViewResourceId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Adds the specified object at the end of the array. |
| 219 | * |
| 220 | * @param object The object to add at the end of the array. |
| 221 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 222 | public void add(@Nullable T object) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 223 | synchronized (mLock) { |
| 224 | if (mOriginalValues != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | mOriginalValues.add(object); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 226 | } else { |
| 227 | mObjects.add(object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 229 | mObjectsFromResources = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 231 | if (mNotifyOnChange) notifyDataSetChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 235 | * Adds the specified Collection at the end of the array. |
| 236 | * |
| 237 | * @param collection The Collection to add at the end of the array. |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 238 | * @throws UnsupportedOperationException if the <tt>addAll</tt> operation |
| 239 | * is not supported by this list |
| 240 | * @throws ClassCastException if the class of an element of the specified |
| 241 | * collection prevents it from being added to this list |
| 242 | * @throws NullPointerException if the specified collection contains one |
| 243 | * or more null elements and this list does not permit null |
| 244 | * elements, or if the specified collection is null |
| 245 | * @throws IllegalArgumentException if some property of an element of the |
| 246 | * specified collection prevents it from being added to this list |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 247 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 248 | public void addAll(@NonNull Collection<? extends T> collection) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 249 | synchronized (mLock) { |
| 250 | if (mOriginalValues != null) { |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 251 | mOriginalValues.addAll(collection); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 252 | } else { |
| 253 | mObjects.addAll(collection); |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 254 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 255 | mObjectsFromResources = false; |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 256 | } |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 257 | if (mNotifyOnChange) notifyDataSetChanged(); |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Adds the specified items at the end of the array. |
| 262 | * |
| 263 | * @param items The items to add at the end of the array. |
| 264 | */ |
| 265 | public void addAll(T ... items) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 266 | synchronized (mLock) { |
| 267 | if (mOriginalValues != null) { |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 268 | Collections.addAll(mOriginalValues, items); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 269 | } else { |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 270 | Collections.addAll(mObjects, items); |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 271 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 272 | mObjectsFromResources = false; |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 273 | } |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 274 | if (mNotifyOnChange) notifyDataSetChanged(); |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | * Inserts the specified object at the specified index in the array. |
| 279 | * |
| 280 | * @param object The object to insert into the array. |
| 281 | * @param index The index at which the object must be inserted. |
| 282 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 283 | public void insert(@Nullable T object, int index) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 284 | synchronized (mLock) { |
| 285 | if (mOriginalValues != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | mOriginalValues.add(index, object); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 287 | } else { |
| 288 | mObjects.add(index, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 290 | mObjectsFromResources = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | } |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 292 | if (mNotifyOnChange) notifyDataSetChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Removes the specified object from the array. |
| 297 | * |
| 298 | * @param object The object to remove. |
| 299 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 300 | public void remove(@Nullable T object) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 301 | synchronized (mLock) { |
| 302 | if (mOriginalValues != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | mOriginalValues.remove(object); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 304 | } else { |
| 305 | mObjects.remove(object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 307 | mObjectsFromResources = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | if (mNotifyOnChange) notifyDataSetChanged(); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Remove all elements from the list. |
| 314 | */ |
| 315 | public void clear() { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 316 | synchronized (mLock) { |
| 317 | if (mOriginalValues != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | mOriginalValues.clear(); |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 319 | } else { |
| 320 | mObjects.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 322 | mObjectsFromResources = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | if (mNotifyOnChange) notifyDataSetChanged(); |
| 325 | } |
| 326 | |
| 327 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 328 | * Sorts the content of this adapter using the specified comparator. |
| 329 | * |
| 330 | * @param comparator The comparator used to sort the objects contained |
| 331 | * in this adapter. |
| 332 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 333 | public void sort(@NonNull Comparator<? super T> comparator) { |
Gilles Debunne | be2c4f9 | 2011-01-17 15:14:32 -0800 | [diff] [blame] | 334 | synchronized (mLock) { |
| 335 | if (mOriginalValues != null) { |
| 336 | Collections.sort(mOriginalValues, comparator); |
| 337 | } else { |
| 338 | Collections.sort(mObjects, comparator); |
| 339 | } |
| 340 | } |
Christian Mehlmauer | 8c582ef | 2010-06-11 22:28:38 +0200 | [diff] [blame] | 341 | if (mNotifyOnChange) notifyDataSetChanged(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | @Override |
| 345 | public void notifyDataSetChanged() { |
| 346 | super.notifyDataSetChanged(); |
| 347 | mNotifyOnChange = true; |
| 348 | } |
| 349 | |
| 350 | /** |
Yigit Boyar | 42c69b9 | 2016-08-19 10:15:52 -0700 | [diff] [blame] | 351 | * Control whether methods that change the list ({@link #add}, {@link #addAll(Collection)}, |
| 352 | * {@link #addAll(Object[])}, {@link #insert}, {@link #remove}, {@link #clear}, |
| 353 | * {@link #sort(Comparator)}) automatically call {@link #notifyDataSetChanged}. If set to |
| 354 | * false, caller must manually call notifyDataSetChanged() to have the changes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | * reflected in the attached view. |
| 356 | * |
| 357 | * The default is true, and calling notifyDataSetChanged() |
| 358 | * resets the flag to true. |
| 359 | * |
| 360 | * @param notifyOnChange if true, modifications to the list will |
| 361 | * automatically call {@link |
| 362 | * #notifyDataSetChanged} |
| 363 | */ |
| 364 | public void setNotifyOnChange(boolean notifyOnChange) { |
| 365 | mNotifyOnChange = notifyOnChange; |
| 366 | } |
| 367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Returns the context associated with this array adapter. The context is used |
| 370 | * to create views from the resource passed to the constructor. |
| 371 | * |
| 372 | * @return The Context associated with this adapter. |
| 373 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 374 | public @NonNull Context getContext() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | return mContext; |
| 376 | } |
| 377 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 378 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | public int getCount() { |
| 380 | return mObjects.size(); |
| 381 | } |
| 382 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 383 | @Override |
| 384 | public @Nullable T getItem(int position) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | return mObjects.get(position); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Returns the position of the specified item in the array. |
| 390 | * |
| 391 | * @param item The item to retrieve the position of. |
| 392 | * |
| 393 | * @return The position of the specified item. |
| 394 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 395 | public int getPosition(@Nullable T item) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | return mObjects.indexOf(item); |
| 397 | } |
| 398 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 399 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | public long getItemId(int position) { |
| 401 | return position; |
| 402 | } |
| 403 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 404 | @Override |
| 405 | public @NonNull View getView(int position, @Nullable View convertView, |
| 406 | @NonNull ViewGroup parent) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 407 | return createViewFromResource(mInflater, position, convertView, parent, mResource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 410 | private @NonNull View createViewFromResource(@NonNull LayoutInflater inflater, int position, |
| 411 | @Nullable View convertView, @NonNull ViewGroup parent, int resource) { |
| 412 | final View view; |
| 413 | final TextView text; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | |
| 415 | if (convertView == null) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 416 | view = inflater.inflate(resource, parent, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } else { |
| 418 | view = convertView; |
| 419 | } |
| 420 | |
| 421 | try { |
| 422 | if (mFieldId == 0) { |
| 423 | // If no custom field is assigned, assume the whole resource is a TextView |
| 424 | text = (TextView) view; |
| 425 | } else { |
| 426 | // Otherwise, find the TextView field within the layout |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 427 | text = view.findViewById(mFieldId); |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 428 | |
| 429 | if (text == null) { |
| 430 | throw new RuntimeException("Failed to find view with ID " |
| 431 | + mContext.getResources().getResourceName(mFieldId) |
| 432 | + " in item layout"); |
| 433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | } catch (ClassCastException e) { |
| 436 | Log.e("ArrayAdapter", "You must supply a resource ID for a TextView"); |
| 437 | throw new IllegalStateException( |
| 438 | "ArrayAdapter requires the resource ID to be a TextView", e); |
| 439 | } |
| 440 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 441 | final T item = getItem(position); |
Daisuke Miyakawa | b5d9132 | 2009-07-09 14:26:56 +0900 | [diff] [blame] | 442 | if (item instanceof CharSequence) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 443 | text.setText((CharSequence) item); |
Daisuke Miyakawa | b5d9132 | 2009-07-09 14:26:56 +0900 | [diff] [blame] | 444 | } else { |
| 445 | text.setText(item.toString()); |
| 446 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | |
| 448 | return view; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * <p>Sets the layout resource to create the drop down views.</p> |
| 453 | * |
| 454 | * @param resource the layout resource defining the drop down views |
| 455 | * @see #getDropDownView(int, android.view.View, android.view.ViewGroup) |
| 456 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 457 | public void setDropDownViewResource(@LayoutRes int resource) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | this.mDropDownResource = resource; |
| 459 | } |
| 460 | |
| 461 | /** |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 462 | * Sets the {@link Resources.Theme} against which drop-down views are |
| 463 | * inflated. |
| 464 | * <p> |
| 465 | * By default, drop-down views are inflated against the theme of the |
| 466 | * {@link Context} passed to the adapter's constructor. |
| 467 | * |
| 468 | * @param theme the theme against which to inflate drop-down views or |
| 469 | * {@code null} to use the theme from the adapter's context |
| 470 | * @see #getDropDownView(int, View, ViewGroup) |
| 471 | */ |
| 472 | @Override |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 473 | public void setDropDownViewTheme(@Nullable Resources.Theme theme) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 474 | if (theme == null) { |
| 475 | mDropDownInflater = null; |
| 476 | } else if (theme == mInflater.getContext().getTheme()) { |
| 477 | mDropDownInflater = mInflater; |
| 478 | } else { |
| 479 | final Context context = new ContextThemeWrapper(mContext, theme); |
| 480 | mDropDownInflater = LayoutInflater.from(context); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | @Override |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 485 | public @Nullable Resources.Theme getDropDownViewTheme() { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 486 | return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme(); |
| 487 | } |
| 488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | @Override |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 490 | public View getDropDownView(int position, @Nullable View convertView, |
| 491 | @NonNull ViewGroup parent) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 492 | final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater; |
| 493 | return createViewFromResource(inflater, position, convertView, parent, mDropDownResource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Creates a new ArrayAdapter from external resources. The content of the array is |
| 498 | * obtained through {@link android.content.res.Resources#getTextArray(int)}. |
| 499 | * |
| 500 | * @param context The application's environment. |
| 501 | * @param textArrayResId The identifier of the array to use as the data source. |
| 502 | * @param textViewResId The identifier of the layout used to create views. |
| 503 | * |
| 504 | * @return An ArrayAdapter<CharSequence>. |
| 505 | */ |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 506 | public static @NonNull ArrayAdapter<CharSequence> createFromResource(@NonNull Context context, |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 507 | @ArrayRes int textArrayResId, @LayoutRes int textViewResId) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 508 | final CharSequence[] strings = context.getResources().getTextArray(textArrayResId); |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 509 | return new ArrayAdapter<>(context, textViewResId, 0, Arrays.asList(strings), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 512 | @Override |
| 513 | public @NonNull Filter getFilter() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | if (mFilter == null) { |
| 515 | mFilter = new ArrayFilter(); |
| 516 | } |
| 517 | return mFilter; |
| 518 | } |
| 519 | |
| 520 | /** |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 521 | * {@inheritDoc} |
| 522 | * |
| 523 | * @return values from the string array used by {@link #createFromResource(Context, int, int)}, |
| 524 | * or {@code null} if object was created otherwsie or if contents were dynamically changed after |
| 525 | * creation. |
| 526 | */ |
| 527 | @Override |
| 528 | public CharSequence[] getAutofillOptions() { |
Felipe Leme | 49a38c6 | 2017-06-13 12:51:43 -0700 | [diff] [blame] | 529 | // First check if app developer explicitly set them. |
| 530 | final CharSequence[] explicitOptions = super.getAutofillOptions(); |
| 531 | if (explicitOptions != null) { |
| 532 | return explicitOptions; |
| 533 | } |
| 534 | |
| 535 | // Otherwise, only return options that came from static resources. |
Felipe Leme | 7e4c205 | 2017-04-18 09:45:58 -0700 | [diff] [blame] | 536 | if (!mObjectsFromResources || mObjects == null || mObjects.isEmpty()) { |
| 537 | return null; |
| 538 | } |
| 539 | final int size = mObjects.size(); |
| 540 | final CharSequence[] options = new CharSequence[size]; |
| 541 | mObjects.toArray(options); |
| 542 | return options; |
| 543 | } |
| 544 | |
| 545 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | * <p>An array filter constrains the content of the array adapter with |
| 547 | * a prefix. Each item that does not start with the supplied prefix |
| 548 | * is removed from the list.</p> |
| 549 | */ |
| 550 | private class ArrayFilter extends Filter { |
| 551 | @Override |
| 552 | protected FilterResults performFiltering(CharSequence prefix) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 553 | final FilterResults results = new FilterResults(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | |
| 555 | if (mOriginalValues == null) { |
| 556 | synchronized (mLock) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 557 | mOriginalValues = new ArrayList<>(mObjects); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
| 561 | if (prefix == null || prefix.length() == 0) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 562 | final ArrayList<T> list; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | synchronized (mLock) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 564 | list = new ArrayList<>(mOriginalValues); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 566 | results.values = list; |
| 567 | results.count = list.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } else { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 569 | final String prefixString = prefix.toString().toLowerCase(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 571 | final ArrayList<T> values; |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 572 | synchronized (mLock) { |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 573 | values = new ArrayList<>(mOriginalValues); |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 574 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | |
Romain Guy | 95a78c3 | 2011-08-15 15:36:30 -0700 | [diff] [blame] | 576 | final int count = values.size(); |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 577 | final ArrayList<T> newValues = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | |
| 579 | for (int i = 0; i < count; i++) { |
| 580 | final T value = values.get(i); |
| 581 | final String valueText = value.toString().toLowerCase(); |
| 582 | |
| 583 | // First match against the whole, non-splitted value |
| 584 | if (valueText.startsWith(prefixString)) { |
| 585 | newValues.add(value); |
| 586 | } else { |
| 587 | final String[] words = valueText.split(" "); |
Alan Viverette | 9e623b6 | 2016-04-01 10:11:25 -0400 | [diff] [blame] | 588 | for (String word : words) { |
| 589 | if (word.startsWith(prefixString)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | newValues.add(value); |
| 591 | break; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | results.values = newValues; |
| 598 | results.count = newValues.size(); |
| 599 | } |
| 600 | |
| 601 | return results; |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | protected void publishResults(CharSequence constraint, FilterResults results) { |
| 606 | //noinspection unchecked |
| 607 | mObjects = (List<T>) results.values; |
| 608 | if (results.count > 0) { |
| 609 | notifyDataSetChanged(); |
| 610 | } else { |
| 611 | notifyDataSetInvalidated(); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |