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?
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
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
Related
I am trying to implement multi-column in ListView. Currently I have used a GridView for each row to give Column look. However I can think of one more approach of making the ListView row contain all the column Layout of Gridview and while binding iterate over the layout and assign data.
Approach #1: ListViewRow-> contains GridView
GridViewRow-> Layout for each column
Approach #2: ListViewRow contains say 12 LinearLayouts each having its own display (display wise all LinearLayouts are same). Similar to this
Which of these approaches will be good in performance terms?
Take a look at relative layout, maybe you can use it in your listviewrow
I have to implement a ListView or GridView where every item will have non-uniform height/width. Also a particular item could span multiple columns. A GridView is perhaps a better candidate, but again each row has a different schematic. The attached image best describes what I want to achieve.
I am looking for ideas on how best to implement this. I am not looking for code samples, but just some guidelines.
Right now, I'm thinking of implementing a custom ListAdapter where each row is laid out in the scheme I want. Of course then I'd have to do some tricks to map list indexes to actually items in array of items (if I use and array adapter). For example, in the mockup below, the penguins will have an index of 1 in my array, but 0 in the ListView.
in these kind of structure you should use a layout in xml and add dynamic view in that layout from java file.
May be a relative Layout with ImageViews is good. It may get a bit messy for the .xml though! This seems quite interesting. Do keep us updated on which of the solutions you finally chose.
Thanks!
I have a dynamically populated TableLayout.
I'm developing for GoogleTV so I need to make the rows in the TableLayout selectable by the remote D-Pad. How can I achieve that?
Am I using the wrong approach here? Should I use a GridView instead? Or maybe ListView Adapter?
I've tried to find examples and similar questions but without success.
Thanks for the help!
How can I make the rows user selectable (and clickable)?
I don't know what do you understand by selectable. To make it clickable just add a OnClickListener to each of your TableRows. To visually signal the user that the row is clickable(like a Button does it when it's pressed) then use a selector drawable for the TableRow's background.
Am I using the wrong approach here? Should I use a GridView instead?
Or maybe ListView Adapter?
A GridView no, but a ListView is a good candidate. If the table doesn't increase past the size you mention(10 x 3) you could use a simple TableLayout. If you know the TableLayout is likely to need to be scrolled(if it needs to be in a ScrollView) then a ListView will be more efficient.
i have to make a listview in which there are two elements to be displayed vertically.
i know that to use the default adapter given with android there can only be one array and one text resource...ie if i am using android.R.layout.simple_list_view then there is only one text resource.
To make a custom Listview i am doing the following:
making a xml layout file for each element of the listview
extending a custom adapter class which extends the baseadapter
in the getview method of the custom adapter class i am inflating the view for each element and then returning with the info i want the listview element to have from an array which i have passed as a constructor to the custom adapter class.
this seems very tedious because there are several instances where i have to make listview where sometimes there are three text elements in each listview element and sometimes 2 text elements in each listview element.
is there an easier way to do the above.
thank you in advance.
With such simple layout, I'd suggest you to just use a LinearLayout and 2-3 TextViews (or any view you need, even an horizontal LinearLayout). Nothing will beat that simplicity. There's no need for a ListView in that case.
You could consider creating a generic, reusable ListView layout file that is loaded up with all the various elements you need (which, hopefully is a concise few). You could default as many of those elements in the layout XML file with android:visible="false" and then programmatically toggle the visibility.
Why can't you just reuse the adapter? It's got plenty of loading/unloading methods associated with it.
Yes, what Aleadam is saying; if you only have a couple of things why use a ListView? TextView would seem a much quicker way to prototype data display!
I have a listview with custom rows and that extends SimpleAdapter.
Each row consist of two linear layouts : 1st having two textviews of which one is hidden in horizontal orientation, second having two textviews in horizontal orientation.
Now depending on the value in hidden textview , I want to setcolor for the remaining items for the row.
To put it as simple:
each listview item has some custom colors the value of which comes from the hidden field.
I have done this by overriding getview() for the simpleadapter and returning view for each, but this makes list very slow to render (and that I think is obvious as so much of work for each view before showing it).
Can I do this in some more efficient way ? like making views and then add up to list instead of using xml layout maybe one solution OR any other ? Any help ? Thanks.
If you use convertView in your adapter, I would not expect you to have any particular speed issues. Creating and garbage collecting rows is expensive -- setting some colors on a set of TextViews is not. So, make sure you are using the convertView parameter to recycle your rows.
Here is a free excerpt from one of my books that covers row recycling.