Yehia Hassan Yehia Hassan - 9 months ago 37
Android Question

What is is the difference between ArrayList.get(int index) and ArrayAdapter.getItem(int position) methods?

I am setting up an

OnItemClickListener
and I can't see the difference between the two methods and they both work just fine, and I can't understand the difference between them from the documentation.

Answer Source

They are defined on different classes - that is the only difference.

The adapter method calls the other.

ArrayAdapter<T> Android Source code.

public T getItem(int position) {
    return mObjects.get(position);
}

You actually don't "need" to store an Arraylist independently in your class.

ArrayAdapter has its own add() method and you call the constructor that doesn't take a list.

public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) {
    this(context, resource, 0, new ArrayList<>());
}