0

I have an ArrayAdapter that's using an ArrayList to display data in a ListView.

During the course of the activity, I sometimes need to edit the ArrayList by adding and deleting items.

Is there a difference if I call the add/delete functions on the actual ArrayAdapter vs. the underlying ArrayList? Which is better to use?

2 Answers 2

1

Use the adapter methods. This will automatically notify your adapter (and thus the bound list) that your data has changed.

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

4 Comments

Oh. So it's the same as editing the backing arraylist, and then calling notifyDataSetChanged()?
By examining the source (grepcode.com/file/repository.grepcode.com/java/ext/…) you can see that for the most part, yes this is true.
So does it matter that if I'm adding lots of items, it's essentially calling notifyDataSetChanged() each time. Is it better to add to underlying arraylist, and only call notifyDataSetChanged() once?
That's certainly something to consider. I was initially just going for the simple straightforward answer, but if you're adding a lot of items, from a performance perspective it may make sense to add them to the ArrayList and call notifyDataSetChanged() yourself, yes.
0

Some times it is necessary (or at least more convenient) to modify the ArrayList (e.g., is a field of some other class, or it is being modified by other thread that does not know about the adapter).

In those cases, you will need to call adapter.notifyDataSetChanged()

Comments

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.