2

I have a simple question. I have an arrayList of views I want to add to a ViewGroup. I currently am iterating through them with a for loop and adding them individually.

ViewGroup commentList = (ViewGroup) this.findViewById(R.id.comment_list);
for (View commment: comments) {
    commentList.addView(commment);
}

Can I do this all at once with a single call? Like an addAll() method? I feel like this would be more efficient, especially if I have a lot of views I'm adding....

3
  • 2
    What is occuring behind the scenes is most likely the same... Speed will not increase by a method such as addAll() but it could clean up your code... Commented Apr 17, 2015 at 20:59
  • yeah i think so you do it but it won't affect your complexity it will remain same Commented Apr 17, 2015 at 21:09
  • I'd say you want to use a ListView + Adapter for that Commented Apr 17, 2015 at 22:58

1 Answer 1

1

Iterating through the views if there are a lot is fairly efficient. If this ViewGroup is going to always have a lot of views you may want to consider using a view type that uses an adapter and recycling / reuse otherwise you'll have a lot of views that are in memory yet are off screen.

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

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.