0

i was working with ListView in android,so to insert items into the list with a button i did the following :created arraylist,arrayadapter,set the adapter to arraylist and used the following code :

addButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            listItems.add(editText.getText().toString());
            adapter.notifyDataSetChanged();
        }
    });

this works fine but i also found that there is a method called insert for the ArrayAdapter so what is the difference between inserting into ArrayAdapter and adding into arrayList ?

PS :im new to this adapter concept can exlpain it to me ? thanks.

0

2 Answers 2

2

In simple understanding you can say that :

arrayList is used for items of the list while work of arrayAdapter is to manage the items of the list.

Rows are created on demand by the adapter as they come on to the screen.

I would recommend you to check out the link if you want to learn more about them https://developer.android.com/reference/android/widget/ArrayAdapter.html https://developer.android.com/reference/java/util/ArrayList.html

Sign up to request clarification or add additional context in comments.

Comments

1

ArrayAdapter is different than ArrayList

Firstly Adapter in Android is:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

See the docs for details and types of Adapters in Android

And ArrayAdapter is:

A concrete BaseAdapter that is backed by an array of arbitrary objects.

But ArrayList is:

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

refer to docs for more info

Also read this answer for more understanding about the difference between both of them

3 Comments

the other answer which you linked was helpful.if i understand it correctly there is a difference between insert into adapter and add to arraylist. so which one should i use ?
@anotherGatsby Have you understood the diffrence between ArrayAdapter and ArrayList? you can not compare two methods from two diffrent classes. Insert is used to insert objects in the Adapter's Array and add of the ArrayList is used to insert objects to the collection ArrayList
i think i'll try out the insert and add methods so that i'll know how they really differ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.