2

There is a requirement to have not-so-trivial dynamic list, each record of which consists of several columns (texts, buttons). It should look something like:

Text11 Text12 Button1 Button2
Text21 Text22 Button1 Button2
...

At first obvious way to accomplish that seemed to be TableLayout. I was expecting to have layout/styling data specified in res/layout/*.xml and to populate it with some dataset from java code (as with ListView, for which its possible to specify TextView of item in *.xml and bind it to some array using ArrayAdapter). But after playing for a while, all I found to be possible is fully populating TableLayout programatically. Still, creating TableRow by TableRow and setting layout attributes directly in java code doesn't seem elegant enough.

So the question is: am I at the right path? Is TableLayout really best View to accomplish that? Maybe it's more appropriate to extend ListView or something else to meet such requirements?

Ralkie
  • 3,736
  • 3
  • 25
  • 25

2 Answers2

4

Using ListView and ArrayAdapter you can do more complicated layouts than just a TextView. You could specify a LinearLayout with 2 TextViews and 2 Buttons for each row in the List.

here's a similar question Android: ListView elements with multiple clickable buttons

Community
  • 1
  • 1
James
  • 577
  • 1
  • 3
  • 14
  • Because of your answer I reconsidered ListView and I've managed to make it work as required, thanks. – Ralkie Jun 17 '10 at 08:51
  • 1
    Still this doesn't answer the question. Is it possible to populate TableLayout with ArrayAdapter – Nayn Jun 25 '10 at 17:02
  • @Nayn - Of course it's *possible*. Is it worth the effort, rather than just using the right tool for the job (ListView)? Probably not, so James' answer satisfies the question. – kiswa Jul 15 '10 at 14:54
  • ListView is a great way to implement a list of items that use an Adapter, but it's not always possible to use it (oh how much I wish it weren't the case). – Artem Russakovskii Jun 22 '11 at 00:08
1

IMHO it depends on the amount of your data you need to render.

Build layout dinamically via inflate/addView is a quite simple task but is also more slow than using a custom adapter. with a custom adapter you can reuse the convertView parameter and then set the values more efficiently

Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63