| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.ArrayRes; |
| 20 |
import android.annotation.IdRes; |
| 21 |
import android.annotation.LayoutRes; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.res.Resources; |
| 27 |
import android.util.Log; |
| 28 |
import android.view.ContextThemeWrapper; |
| 29 |
import android.view.LayoutInflater; |
| 30 |
import android.view.View; |
| 31 |
import android.view.ViewGroup; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.Collection; |
| 36 |
import java.util.Collections; |
| 37 |
import java.util.Comparator; |
| 38 |
import java.util.List; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@UnsupportedAppUsage |
| 79 |
private final Object mLock = new Object(); |
| 80 |
|
| 81 |
private final LayoutInflater mInflater; |
| 82 |
|
| 83 |
private final Context mContext; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private final int mResource; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private int mDropDownResource; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@UnsupportedAppUsage |
| 102 |
private List<T> mObjects; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private boolean mObjectsFromResources; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private int mFieldId = 0; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private boolean mNotifyOnChange = true; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@UnsupportedAppUsage |
| 125 |
private ArrayList<T> mOriginalValues; |
| 126 |
private ArrayFilter mFilter; |
| 127 |
|
| 128 |
|
| 129 |
private LayoutInflater mDropDownInflater; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) { |
| 139 |
this(context, resource, 0, new ArrayList<>()); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 151 |
@IdRes int textViewResourceId) { |
| 152 |
this(context, resource, textViewResourceId, new ArrayList<>()); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) { |
| 165 |
this(context, resource, 0, Arrays.asList(objects)); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 179 |
@IdRes int textViewResourceId, @NonNull T[] objects) { |
| 180 |
this(context, resource, textViewResourceId, Arrays.asList(objects)); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 192 |
@NonNull List<T> objects) { |
| 193 |
this(context, resource, 0, objects); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 206 |
@IdRes int textViewResourceId, @NonNull List<T> objects) { |
| 207 |
this(context, resource, textViewResourceId, objects, false); |
| 208 |
} |
| 209 |
|
| 210 |
private ArrayAdapter(@NonNull Context context, @LayoutRes int resource, |
| 211 |
@IdRes int textViewResourceId, @NonNull List<T> objects, boolean objsFromResources) { |
| 212 |
mContext = context; |
| 213 |
mInflater = LayoutInflater.from(context); |
| 214 |
mResource = mDropDownResource = resource; |
| 215 |
mObjects = objects; |
| 216 |
mObjectsFromResources = objsFromResources; |
| 217 |
mFieldId = textViewResourceId; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public void add(@Nullable T object) { |
| 227 |
synchronized (mLock) { |
| 228 |
if (mOriginalValues != null) { |
| 229 |
mOriginalValues.add(object); |
| 230 |
} else { |
| 231 |
mObjects.add(object); |
| 232 |
} |
| 233 |
mObjectsFromResources = false; |
| 234 |
} |
| 235 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public void addAll(@NonNull Collection<? extends T> collection) { |
| 253 |
synchronized (mLock) { |
| 254 |
if (mOriginalValues != null) { |
| 255 |
mOriginalValues.addAll(collection); |
| 256 |
} else { |
| 257 |
mObjects.addAll(collection); |
| 258 |
} |
| 259 |
mObjectsFromResources = false; |
| 260 |
} |
| 261 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public void addAll(T ... items) { |
| 271 |
synchronized (mLock) { |
| 272 |
if (mOriginalValues != null) { |
| 273 |
Collections.addAll(mOriginalValues, items); |
| 274 |
} else { |
| 275 |
Collections.addAll(mObjects, items); |
| 276 |
} |
| 277 |
mObjectsFromResources = false; |
| 278 |
} |
| 279 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public void insert(@Nullable T object, int index) { |
| 290 |
synchronized (mLock) { |
| 291 |
if (mOriginalValues != null) { |
| 292 |
mOriginalValues.add(index, object); |
| 293 |
} else { |
| 294 |
mObjects.add(index, object); |
| 295 |
} |
| 296 |
mObjectsFromResources = false; |
| 297 |
} |
| 298 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public void remove(@Nullable T object) { |
| 308 |
synchronized (mLock) { |
| 309 |
if (mOriginalValues != null) { |
| 310 |
mOriginalValues.remove(object); |
| 311 |
} else { |
| 312 |
mObjects.remove(object); |
| 313 |
} |
| 314 |
mObjectsFromResources = false; |
| 315 |
} |
| 316 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public void clear() { |
| 325 |
synchronized (mLock) { |
| 326 |
if (mOriginalValues != null) { |
| 327 |
mOriginalValues.clear(); |
| 328 |
} else { |
| 329 |
mObjects.clear(); |
| 330 |
} |
| 331 |
mObjectsFromResources = false; |
| 332 |
} |
| 333 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public void sort(@NonNull Comparator<? super T> comparator) { |
| 343 |
synchronized (mLock) { |
| 344 |
if (mOriginalValues != null) { |
| 345 |
Collections.sort(mOriginalValues, comparator); |
| 346 |
} else { |
| 347 |
Collections.sort(mObjects, comparator); |
| 348 |
} |
| 349 |
} |
| 350 |
if (mNotifyOnChange) notifyDataSetChanged(); |
| 351 |
} |
| 352 |
|
| 353 |
@Override |
| 354 |
public void notifyDataSetChanged() { |
| 355 |
super.notifyDataSetChanged(); |
| 356 |
mNotifyOnChange = true; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public void setNotifyOnChange(boolean notifyOnChange) { |
| 374 |
mNotifyOnChange = notifyOnChange; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public @NonNull Context getContext() { |
| 384 |
return mContext; |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public int getCount() { |
| 389 |
return mObjects.size(); |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
public @Nullable T getItem(int position) { |
| 394 |
return mObjects.get(position); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public int getPosition(@Nullable T item) { |
| 405 |
return mObjects.indexOf(item); |
| 406 |
} |
| 407 |
|
| 408 |
@Override |
| 409 |
public long getItemId(int position) { |
| 410 |
return position; |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public @NonNull View getView(int position, @Nullable View convertView, |
| 415 |
@NonNull ViewGroup parent) { |
| 416 |
return createViewFromResource(mInflater, position, convertView, parent, mResource); |
| 417 |
} |
| 418 |
|
| 419 |
private @NonNull View createViewFromResource(@NonNull LayoutInflater inflater, int position, |
| 420 |
@Nullable View convertView, @NonNull ViewGroup parent, int resource) { |
| 421 |
final View view; |
| 422 |
final TextView text; |
| 423 |
|
| 424 |
if (convertView == null) { |
| 425 |
view = inflater.inflate(resource, parent, false); |
| 426 |
} else { |
| 427 |
view = convertView; |
| 428 |
} |
| 429 |
|
| 430 |
try { |
| 431 |
if (mFieldId == 0) { |
| 432 |
|
| 433 |
text = (TextView) view; |
| 434 |
} else { |
| 435 |
|
| 436 |
text = view.findViewById(mFieldId); |
| 437 |
|
| 438 |
if (text == null) { |
| 439 |
throw new RuntimeException("Failed to find view with ID " |
| 440 |
+ mContext.getResources().getResourceName(mFieldId) |
| 441 |
+ " in item layout"); |
| 442 |
} |
| 443 |
} |
| 444 |
} catch (ClassCastException e) { |
| 445 |
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView"); |
| 446 |
throw new IllegalStateException( |
| 447 |
"ArrayAdapter requires the resource ID to be a TextView", e); |
| 448 |
} |
| 449 |
|
| 450 |
final T item = getItem(position); |
| 451 |
if (item instanceof CharSequence) { |
| 452 |
text.setText((CharSequence) item); |
| 453 |
} else { |
| 454 |
text.setText(item.toString()); |
| 455 |
} |
| 456 |
|
| 457 |
return view; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public void setDropDownViewResource(@LayoutRes int resource) { |
| 467 |
this.mDropDownResource = resource; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
@Override |
| 482 |
public void setDropDownViewTheme(@Nullable Resources.Theme theme) { |
| 483 |
if (theme == null) { |
| 484 |
mDropDownInflater = null; |
| 485 |
} else if (theme == mInflater.getContext().getTheme()) { |
| 486 |
mDropDownInflater = mInflater; |
| 487 |
} else { |
| 488 |
final Context context = new ContextThemeWrapper(mContext, theme); |
| 489 |
mDropDownInflater = LayoutInflater.from(context); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
@Override |
| 494 |
public @Nullable Resources.Theme getDropDownViewTheme() { |
| 495 |
return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme(); |
| 496 |
} |
| 497 |
|
| 498 |
@Override |
| 499 |
public View getDropDownView(int position, @Nullable View convertView, |
| 500 |
@NonNull ViewGroup parent) { |
| 501 |
final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater; |
| 502 |
return createViewFromResource(inflater, position, convertView, parent, mDropDownResource); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public static @NonNull ArrayAdapter<CharSequence> createFromResource(@NonNull Context context, |
| 516 |
@ArrayRes int textArrayResId, @LayoutRes int textViewResId) { |
| 517 |
final CharSequence[] strings = context.getResources().getTextArray(textArrayResId); |
| 518 |
return new ArrayAdapter<>(context, textViewResId, 0, Arrays.asList(strings), true); |
| 519 |
} |
| 520 |
|
| 521 |
@Override |
| 522 |
public @NonNull Filter getFilter() { |
| 523 |
if (mFilter == null) { |
| 524 |
mFilter = new ArrayFilter(); |
| 525 |
} |
| 526 |
return mFilter; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
@Override |
| 537 |
public CharSequence[] getAutofillOptions() { |
| 538 |
|
| 539 |
final CharSequence[] explicitOptions = super.getAutofillOptions(); |
| 540 |
if (explicitOptions != null) { |
| 541 |
return explicitOptions; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
if (!mObjectsFromResources || mObjects == null || mObjects.isEmpty()) { |
| 546 |
return null; |
| 547 |
} |
| 548 |
final int size = mObjects.size(); |
| 549 |
final CharSequence[] options = new CharSequence[size]; |
| 550 |
mObjects.toArray(options); |
| 551 |
return options; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
private class ArrayFilter extends Filter { |
| 560 |
@Override |
| 561 |
protected FilterResults performFiltering(CharSequence prefix) { |
| 562 |
final FilterResults results = new FilterResults(); |
| 563 |
|
| 564 |
if (mOriginalValues == null) { |
| 565 |
synchronized (mLock) { |
| 566 |
mOriginalValues = new ArrayList<>(mObjects); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if (prefix == null || prefix.length() == 0) { |
| 571 |
final ArrayList<T> list; |
| 572 |
synchronized (mLock) { |
| 573 |
list = new ArrayList<>(mOriginalValues); |
| 574 |
} |
| 575 |
results.values = list; |
| 576 |
results.count = list.size(); |
| 577 |
} else { |
| 578 |
final String prefixString = prefix.toString().toLowerCase(); |
| 579 |
|
| 580 |
final ArrayList<T> values; |
| 581 |
synchronized (mLock) { |
| 582 |
values = new ArrayList<>(mOriginalValues); |
| 583 |
} |
| 584 |
|
| 585 |
final int count = values.size(); |
| 586 |
final ArrayList<T> newValues = new ArrayList<>(); |
| 587 |
|
| 588 |
for (int i = 0; i < count; i++) { |
| 589 |
final T value = values.get(i); |
| 590 |
final String valueText = value.toString().toLowerCase(); |
| 591 |
|
| 592 |
|
| 593 |
if (valueText.startsWith(prefixString)) { |
| 594 |
newValues.add(value); |
| 595 |
} else { |
| 596 |
final String[] words = valueText.split(" "); |
| 597 |
for (String word : words) { |
| 598 |
if (word.startsWith(prefixString)) { |
| 599 |
newValues.add(value); |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
results.values = newValues; |
| 607 |
results.count = newValues.size(); |
| 608 |
} |
| 609 |
|
| 610 |
return results; |
| 611 |
} |
| 612 |
|
| 613 |
@Override |
| 614 |
protected void publishResults(CharSequence constraint, FilterResults results) { |
| 615 |
|
| 616 |
mObjects = (List<T>) results.values; |
| 617 |
if (results.count > 0) { |
| 618 |
notifyDataSetChanged(); |
| 619 |
} else { |
| 620 |
notifyDataSetInvalidated(); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
} |