Code for filtering in ArrayAdapter I have created a code for filtering a list of strings and redisplaying this list. I check if string is starting with a certain letter.
It seems to work, but I wonder if I did all right? Havent I forgot about something?

public class AlternateRowArrayAdapter extends ArrayAdapter
{
    private int[] colors;
    private List&lt;String&gt; list = null;
    private ArrayList&lt;String&gt; arrayList;

    public AlternateRowArrayAdapter(@NonNull Context context, @LayoutRes int layout,
                                    @IdRes int textViewResourceId, @NonNull List&lt;String&gt; objects)
    {
        super(context, layout, textViewResourceId, objects);
        colors = new int[] {ResourcesCompat.getColor(context.getResources(), R.color.white, null) ,
                            ResourcesCompat.getColor(context.getResources(), R.color.lmsLightGrey, null)};
        this.list = objects;
        this.arrayList = new ArrayList&lt;&gt;();
        this.arrayList.addAll(this.list);
    }
    /**
     * Display rows in alternating colors
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        int colorPos = position % colors.length;
        view.setBackgroundColor(colors[colorPos]);
        return view;
    }

    @Nullable
    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    public void filter(String letter)
    {
        letter = letter.toLowerCase();
        list.clear();
        if (letter == null || letter == "")
        {
            list.addAll(arrayList);
        }
        else
        {
            for (String item : arrayList)
            {
                if (item.toLowerCase().startsWith(letter))
                {
                    list.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }


Also it supposed to be dual color rows - but this part works fine.

I have forgot about this part:

@Override
public int getCount()
{
    return filteredItems.size();
}


Without this in getItem there was an OutOfRangeException
Use final properties


private List&lt;String&gt; list = null;
private ArrayList&lt;String&gt; arrayList;



Both properties are set in the constructor, so they should both be final.

private final List&lt;String&gt; list = new ArrayList&lt;&gt;();
private final ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();

public AlternateRowArrayAdapter(@NonNull Context context, @LayoutRes int layout,
                                @IdRes int textViewResourceId, @NonNull List&lt;String&gt; objects)
{
   // ...
    this.list.addAll(objects); // same solution for both lists!
    this.arrayList.addAll(this.list);
}




Choose better names

Naming is the hardest part in programming. Always take your time to think about the names of your identifiers, if they express their purpose.

E.G.: Your lists should have better names like allItems and filteredItems

Work in constructors

In general constructors should not access any method on a dependency or and non private or non final method inside its own class (except super() and this()). 

Therefore I'd rather pass in the colors array as constructor parameter. But I don't know much about android programming to say if that is even possible... 


  Regarding using colors in constructors - it's possible to pass them as an arguement, but we already pass a context as an argument which contains these colors – Marek Czaplicki


Because you passed the context instead of the color array you class has two more dependencies: the context itself and the ResourcesCompat class. You class should have as less dependencies as possible.