Clean up ArrayAdapter lint warnings and annotations

Refactoring only, no code changes.

Change-Id: Ia384a6597c9af84d23aed481f84d67ae817b4ac7
This commit is contained in:
Alan Viverette
2016-04-01 10:11:25 -04:00
parent ba25767b1b
commit 9e623b64b9

View File

@@ -20,6 +20,7 @@ import android.annotation.ArrayRes;
import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
@@ -61,17 +62,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
private final LayoutInflater mInflater;
/**
* 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.
*/
private List<T> mObjects;
private final Context mContext;
/**
* The resource indicating what views to inflate to display the content of this
* array adapter.
*/
private int mResource;
private final int mResource;
/**
* The resource indicating what views to inflate to display the content of this
@@ -80,7 +77,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
private int mDropDownResource;
/**
* If the inflated resource is not a TextView, {@link #mFieldId} is used to find
* 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.
*/
private List<T> mObjects;
/**
* If the inflated resource is not a TextView, {@code mFieldId} is used to find
* a TextView inside the inflated views hierarchy. This field must contain the
* identifier that matches the one defined in the resource file.
*/
@@ -92,8 +95,6 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*/
private boolean mNotifyOnChange = true;
private Context mContext;
// A copy of the original mObjects array, initialized from and then used instead as soon as
// the mFilter ArrayFilter is used. mObjects will then only contain the filtered values.
private ArrayList<T> mOriginalValues;
@@ -109,8 +110,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
*/
public ArrayAdapter(Context context, @LayoutRes int resource) {
this(context, resource, 0, new ArrayList<T>());
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) {
this(context, resource, 0, new ArrayList<>());
}
/**
@@ -121,8 +122,9 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* instantiating views.
* @param textViewResourceId The id of the TextView within the layout resource to be populated
*/
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId) {
this(context, resource, textViewResourceId, new ArrayList<T>());
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@IdRes int textViewResourceId) {
this(context, resource, textViewResourceId, new ArrayList<>());
}
/**
@@ -133,7 +135,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull T[] objects) {
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
this(context, resource, 0, Arrays.asList(objects));
}
@@ -146,8 +148,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @param textViewResourceId The id of the TextView within the layout resource to be populated
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
@NonNull T[] objects) {
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@IdRes int textViewResourceId, @NonNull T[] objects) {
this(context, resource, textViewResourceId, Arrays.asList(objects));
}
@@ -159,7 +161,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull List<T> objects) {
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@NonNull List<T> objects) {
this(context, resource, 0, objects);
}
@@ -172,8 +175,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @param textViewResourceId The id of the TextView within the layout resource to be populated
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
@NonNull List<T> objects) {
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@IdRes int textViewResourceId, @NonNull List<T> objects) {
mContext = context;
mInflater = LayoutInflater.from(context);
mResource = mDropDownResource = resource;
@@ -186,7 +189,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*
* @param object The object to add at the end of the array.
*/
public void add(T object) {
public void add(@Nullable T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(object);
@@ -201,8 +204,17 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* Adds the specified Collection at the end of the array.
*
* @param collection The Collection to add at the end of the array.
* @throws UnsupportedOperationException if the <tt>addAll</tt> operation
* is not supported by this list
* @throws ClassCastException if the class of an element of the specified
* collection prevents it from being added to this list
* @throws NullPointerException if the specified collection contains one
* or more null elements and this list does not permit null
* elements, or if the specified collection is null
* @throws IllegalArgumentException if some property of an element of the
* specified collection prevents it from being added to this list
*/
public void addAll(Collection<? extends T> collection) {
public void addAll(@NonNull Collection<? extends T> collection) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.addAll(collection);
@@ -235,7 +247,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @param object The object to insert into the array.
* @param index The index at which the object must be inserted.
*/
public void insert(T object, int index) {
public void insert(@Nullable T object, int index) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(index, object);
@@ -251,7 +263,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*
* @param object The object to remove.
*/
public void remove(T object) {
public void remove(@Nullable T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.remove(object);
@@ -282,7 +294,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @param comparator The comparator used to sort the objects contained
* in this adapter.
*/
public void sort(Comparator<? super T> comparator) {
public void sort(@NonNull Comparator<? super T> comparator) {
synchronized (mLock) {
if (mOriginalValues != null) {
Collections.sort(mOriginalValues, comparator);
@@ -293,9 +305,6 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
if (mNotifyOnChange) notifyDataSetChanged();
}
/**
* {@inheritDoc}
*/
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
@@ -326,21 +335,17 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*
* @return The Context associated with this adapter.
*/
public Context getContext() {
public @NonNull Context getContext() {
return mContext;
}
/**
* {@inheritDoc}
*/
@Override
public int getCount() {
return mObjects.size();
}
/**
* {@inheritDoc}
*/
public T getItem(int position) {
@Override
public @Nullable T getItem(int position) {
return mObjects.get(position);
}
@@ -351,28 +356,25 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*
* @return The position of the specified item.
*/
public int getPosition(T item) {
public int getPosition(@Nullable T item) {
return mObjects.indexOf(item);
}
/**
* {@inheritDoc}
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* {@inheritDoc}
*/
public View getView(int position, View convertView, ViewGroup parent) {
@Override
public @NonNull View getView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
return createViewFromResource(mInflater, position, convertView, parent, mResource);
}
private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
ViewGroup parent, int resource) {
View view;
TextView text;
private @NonNull View createViewFromResource(@NonNull LayoutInflater inflater, int position,
@Nullable View convertView, @NonNull ViewGroup parent, int resource) {
final View view;
final TextView text;
if (convertView == null) {
view = inflater.inflate(resource, parent, false);
@@ -387,6 +389,12 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
if (text == null) {
throw new RuntimeException("Failed to find view with ID "
+ mContext.getResources().getResourceName(mFieldId)
+ " in item layout");
}
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
@@ -394,9 +402,9 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
"ArrayAdapter requires the resource ID to be a TextView", e);
}
T item = getItem(position);
final T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
text.setText((CharSequence) item);
} else {
text.setText(item.toString());
}
@@ -426,7 +434,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
* @see #getDropDownView(int, View, ViewGroup)
*/
@Override
public void setDropDownViewTheme(Resources.Theme theme) {
public void setDropDownViewTheme(@Nullable Resources.Theme theme) {
if (theme == null) {
mDropDownInflater = null;
} else if (theme == mInflater.getContext().getTheme()) {
@@ -438,12 +446,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
}
@Override
public Resources.Theme getDropDownViewTheme() {
public @Nullable Resources.Theme getDropDownViewTheme() {
return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme();
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
public View getDropDownView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater;
return createViewFromResource(inflater, position, convertView, parent, mDropDownResource);
}
@@ -458,16 +467,14 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
*
* @return An ArrayAdapter<CharSequence>.
*/
public static ArrayAdapter<CharSequence> createFromResource(Context context,
public static @NonNull ArrayAdapter<CharSequence> createFromResource(@NonNull Context context,
@ArrayRes int textArrayResId, @LayoutRes int textViewResId) {
CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
final CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new ArrayAdapter<>(context, textViewResId, strings);
}
/**
* {@inheritDoc}
*/
public Filter getFilter() {
@Override
public @NonNull Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
@@ -482,31 +489,31 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
final FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<T>(mObjects);
mOriginalValues = new ArrayList<>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<T> list;
final ArrayList<T> list;
synchronized (mLock) {
list = new ArrayList<T>(mOriginalValues);
list = new ArrayList<>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();
final String prefixString = prefix.toString().toLowerCase();
ArrayList<T> values;
final ArrayList<T> values;
synchronized (mLock) {
values = new ArrayList<T>(mOriginalValues);
values = new ArrayList<>(mOriginalValues);
}
final int count = values.size();
final ArrayList<T> newValues = new ArrayList<T>();
final ArrayList<T> newValues = new ArrayList<>();
for (int i = 0; i < count; i++) {
final T value = values.get(i);
@@ -517,11 +524,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
for (String word : words) {
if (word.startsWith(prefixString)) {
newValues.add(value);
break;
}