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!
Related
My problem is simple: I need to make a layout similar to android.R.layout.simple_expandable_list_item_1 which can fit more than one textview in the layout, as well as show the little '>' symbol that indicates that the tab is expandable.
How can this best be accomplished? My first thought was to use the layout as a background of a linear layout with two textviews in it, but I can't seem to make that work.
Vogella has a pretty good tutorial on how to achieve custom list views through Adapters.
Basically, what you do is you create a Layout with the looks you want for the list items and then you extend an adapter (ArrayAdapter for instance), see section 4. Then, in your activity, you create an instance of your Adapter implementation and populate it from an Array containing the data for the list.
Also have a look at the Developers site for how to make the list scroll smoothly with the ViewHolder pattern.
And if you feel you need more info on the expandable part maybe the hive can help.
I have the following prototype:
What is the way to list views in a scroll-able way in a screen?
What I have already tried:
I tried to create a class and extends the LinearLayout class and dynamically adds my Views (the compound controls class is my view also extends LinearLayout) to the main LinearLayout. So I got a list of my views which is scroll-able.
I'm not sure what you mean, but looking at what you have tried, what's wrong with using a ListView? As the name implies, it should do exactly what you want:
What is the way to list views in a scroll-able way in a screen?
Here is a quote from the android documentation of a ListView
ListView is a view group that displays a list of scrollable items. The
list items are automatically inserted to the list using an Adapter
that pulls content from a source such as an array or database query
and converts each item result into a view that's placed into the list.
You should be able to add a ListView directly as a sub-view under your LinearLayout.
your questions seems like hard to understand.
did you mean how to set the whole page(activity) into a listview?
listviews are scrollable in default (of course, you can see it if the list is long enough and you can set the whole activity as a list).. you can customize it also by using templates (xml)
and extending ArrayAdapter of your type (let's say ArrayAdapter<UpcomingEvents>) and supply data from db...
is this what you mean?
hi I want to use a listview (probably) is it possible to fill each entry with say two buttons two text areas, and have them laid out with realtive or linear layouts?
I have used a scroll view with layout inflator to achieve this at the moment, but I'm thinking listview would be better maybe?
yes you can. you need to use a custom list view for that. making an activity by adding a listview in it and then referencing another xml to that listview using an AAdapter so that every element of the listview has the layout of the second xml file. this tutorial should help you understand the idea.
Yes you can place any widgets for the particular List Item for your listview.
For defining that kind of ListView, you have to define a custom adapter, follow the below steps:
Define a one row file for the list item, say for example, RelativeLayout with 2-3 TextViews.
Define a class by extending BaseAdapter.
Inflate the XML and do display operations inside the getView() method of this class.
Set this adapter to the ListView.
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
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.