I'm using a ListView to show a list of items. These items are in a table format with columns and rows. Is there a table like adapter to make sure all the columns and rows line up? I know this brings in the complexity of how large each column should be, what to do with cut off text, and other things. I'm just curious if there is currently and adapter hiding somewhere for this task. Or maybe even another control?
-
A faced the same issue and created a TableView that handles like the known ListView (e.g. applying data with an adapter). If you would like to have a look on it or use it in your project feel free you can find it on [GitHub](https://github.com/ISchwarz23/SortableTableView). – Ingo Schwarz Jul 30 '15 at 19:39
4 Answers
The point of using ListView is to be able to scale to larger data sets by not having to create and layout views for all of the items up-front. Because of this, your request fundamentally conflicts with how ListView works -- ListView simply doesn't know how all of its items will layout, so there is no way for it to automatically make sure they align in some way.
You can ensure they align yourself just by writing the item layout appropriately. For example, very often in the UI you will have an icon followed by a label. If you ensure the icon is a specific size, then all of the list items will align. If you are dealing with elements that are more dynamic like text, you could do the same thing by enforcing up-front a specific width for those elements.
If you really want to have the UI compute the element sizes dynamically and align all of the rows based on them, that is what TableLayout does. It can do this because it always has all elements there to layout together. If you want to allow scrolling in it, you can wrap that in a ScrollView like another poster suggested. Just be aware that this approach will quickly fall apart as your number of rows increases significantly.

- 170,088
- 45
- 397
- 571

- 90,665
- 16
- 140
- 154
-
Up front sizes sound like the best way to go because the data is going to be a considerably large data set. Thanks – Spidy Apr 22 '11 at 16:08
-
1Why do you assume that everyone needs to align based on the entire data set? You're right, that would be too slow. The whole point of alignment is to present a visually appealing presentation of items that are *on the screen*. So simply align with what's shown on the screen. – AlexPi Sep 10 '13 at 15:56
-
@AlexPi So when you scroll the items start to due to alignment changes? – rozina Apr 07 '17 at 08:28
You can use a ListView
or a ListFragment
and populate items using each time a single TableRow
inside a TableLayout
(maybe using android:stretchColumns="0")
you'll have a TableLayout
per line, so it's probably inefficient but it does what you are trying to do

- 4,890
- 2
- 34
- 50
There is GridView for that, but afaik it doesn't work with columns and rows. Luckily you seem to have been expecting some complexity :)

- 15,087
- 7
- 65
- 82
-
Ya, I played around with the GridView but it forces each column to be the same size. I'm thinking a Custom Adapter for the list view may be necessary – Spidy Apr 22 '11 at 15:53