Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView or TableLayout?

I am really confused now as to which one to learn. I am an iPhone app developer and now learning Android development.

I have learnt how to use a ListView with a static array of strings using an Adapter. I am used to using custom cells in iPhone, mostly for showing dynamic content like images and text in TableViews.

So which is the way to go for doing that in Android? TableLayout or ListView?

like image 335
Akash Malhotra Avatar asked Aug 28 '12 04:08

Akash Malhotra


People also ask

What do you mean by ListView?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.

What is ListView explain it using appropriate examples?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.


2 Answers

As others have already said in comments, you need to clearly define what you want to do first before making a concrete decision on which type of layout to use. However, I can certainly understand the confusion that arises from trying to decide over which type of layout class to use, because there are often several to choose from to achieve the same objective. For example, to create a vertically-scrolling list of items, you might at first choose a vertical LinearLayout, which you'd then place inside a ScrollView. But on the other hand, to achieve a similar end result, you could use a ListView together with a suitable Adapter.

Similarly, to show a grid of items that can scroll vertically, you might use a TableLayout inside a ScrollView. Or, a similar result could be achieved from using a GridView, again by supplying data through a suitable Adapter.

Now, the first key difference is this: Classes like LinearLayout and TableLayout require you to supply the child elements either in XML layouts or alternatively programmatically in code. Classes like ListView and GridView (and several others) are very different because they are subclasses of android.widget.AdapterView. The special thing about AdapterView classes is that an Adapter is used to bind data to them. So, going back to the example of a vertical list of items, if you were showing a group of child list items inside a LinearLayout based upon some array data, you would have to programmatically create and add child Views into that LinearLayout based upon the array data. With ListView on the other hand, the individual Views that represent the child items are supplied from a suitable Adapter. So, rather than you programmatically filling the layout with all child items (as would be the case with LinearLayout or TableLayout for instance), an Adapter-based layout instead calls the Adapter to obtain the child Views as and when it needs them.

That last point is the next key difference I believe you should understand about Adapter based layouts: They are much more efficient at showing large amounts of data, in situations where much of the data is scrolled out of view. For example, a ListView is much more efficient to use for displaying a large scrolling list of items than would be if you simply populated a LinearLayout with all the items and put it inside a ScrollView. The reason for this efficiency is that AdapterView-based layouts do not generally contain all child Views all at once. Instead, as the user scrolls through the list, existing child views are "recycled" or "converted" by the Adapter to show the next child elements. To illustrate this with an example: You want a scrolling vertical list of 100 items. The screen may only be large enough to display 7 at once, however. Imagine you use a LinearLayout inside a ScrollView to show 100 list items. That means the LinearLayout container has 100 child Views. Those children are always present in the layout and need to be processed by the system during scroll events, even if only seven can be in view on the screen at one time. This takes extra CPU time, a considerable amount of RAM, and scrolling may be sluggish. Now, with a ListView, the layout will only probably contain just 7 or 8 child Views. As the user scrolls, those child Views are dynamically converted or re-instantiated by the Adapter through which you bind your data. The user will experience a faster, smoother scrolling operation. From a programming point of view, it is usually far more elegant to bind lists of data through an Adapter. When you're dealing with scrolling lists or grids of Bitmaps, the memory constraints of an Android device also mean the use of an AdapterView is pretty much essential.

Bear in mind that when answering this, I've made the assumption that you're trying to show a vertical or tabular list of items that is scrollable, perhaps including Bitmaps, and I'm concentrating on the type of layout that you'd use for achieving the layout and scrolling of that data. Layout classes like LinearLayout, TableLayout, etc. however are important classes that you will use all the time to form individual layout building blocks for your applications. If your entire list is guaranteed to fit into the screen and won't be scrollable, then the additional complexity of using an Adapter (not that it is really that complicated) may be pointless and you might then just want to use a TableLayout or whatever.

like image 115
Trevor Avatar answered Sep 24 '22 03:09

Trevor


The Android equivalent to iOS UITableView is RecyclerView.

It is very powerful and can be filled with data from different sources depending on the type of Adapter you attach to it. You might want to have a look at this RecyclerView codelab.

like image 26
keyboardsurfer Avatar answered Sep 24 '22 03:09

keyboardsurfer