Search code examples
androidandroid-listviewandroid-arrayadapter

android - is array adapter compulsory to use?


Context :

Android dev.

Question :

Is the use of an array adapter absolutely needed for performance/efficiency reasons ?

Couldn't I use a for loop and add the views to the parent myself ?

final View v = inflater.inflate(R.layout.first_fragment, container, false);

LinearLayout sub = (LinearLayout) v.findViewById(R.id.add_me);

LinearLayout vir = (LinearLayout) inflater.inflate(R.layout.vir, sub, false);

// get data from singleton; All the arrays have the same length
for(int x = 0, l = aSingletonArrayLength; x < l; x++){

  //find the ids in vir and populate them
  sub.addView(vir);
}

Solution

  • Yes because array adapter only create the number max of visible views at any time.

    If your screen can only display 4 of your views, only 4 views will be created. Your views are recycled and reused with a new content in future.

    If you create your own views, for 4 elements you will not see the difference, but if you work with 100 of 1000 elements your will create 1000 views instead of 4