I need to implement listview with multiple headers and list item under each header are different.
For example the first header name is weather, under this header, each list item has city name and current temprature.
The second header name is contact, and the each item under this header contains person name, contact number, call icon, message icon etc.
Can anybody know how to implement this in android?
Thanks
Mindus
In order to achieve Multiple header and different layout in a ListView you should use Section ListView
Section is like Header
And you can inflate different layout. Smiler example is given here Link
For Complete Source code go through the below link.
http://amitandroid.blogspot.in/2013/05/android-section-listview.html
As far as I know ListView can have multiple headers, but you can't locate them at the postion you want.The all will be located at the top http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html
I advice you to use separators.Also this can help you https://github.com/commonsguy/cwac-merge
With this MergeAdapter you can insert adapters and views in whatever order you want and then present them all as a single adapter to a listview and consequently you can achieve multiple headers simulataion.
you will have to make different layouts for each type of cell (separate for weather and separate for contacts) , you will have to override getView method of list view as well and upon need, just set your desired view for cell to update it.
Thanks guys, Finally i got it with two different listview with separate linearlayouts. And use textview as a header in each linear layout.
I would suggest not using a straight linear layout for entire sections.
For headers and lists, I would recommend a MergedAdapter, https://github.com/commonsguy/cwac-merge, either that or roll your own.
For multiple cell layout types, this is support in the listviewadapter with the following methods from BaseAdapter
getItemViewType(int position)
getViewTypeCount()
This allows you to specify how many different types of layout are in use and then recycle the layouts appropriately.
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'm looking at adding a listview to my Android application with a thumbnail image. The thumbnail on the left and text on the right.
Is there any way of populating the list view with seperate xml files, for example, could I have separate xmls for Austria, UK, USA etc and add them to the listview?
The reason I ask is, I plan to reuse the individual countries multiple times, as in, shown in an A to Z but then again by Continent etc.
If this is possible, could I get an example of how this is done or a link with a tutorial etc?
Thanks
Do you really want separate .xml files? Or do you want to reuse one .xml file and just replace its content?
If you want separate .xml files (meaning multiple different Views in your Adapter)
take a look at the Adapters methods:
getViewTypeCount() - returns how many different view layouts you have
getItemViewType(int pos) - returns correct view type id by position
With these methods you can specify the number of different types in your adapter, and load different layout files depending on the type.
You could for example have an Adapter like this supporting multiple layouts. (e.g. one item being an actual item, the other one being a separator between the items)
For more specific information, please take a look at this great tutorial:
http://android.amberfog.com/?p=296
Hi,
I want to implement form like in the image, dont have idea how they are adding Fields dynamically. Is this a ListView? Expandable List? user can add and remove at runtime. I have checked Expandable List which contains child items. but we define child in array, In the image they add dynamically.
Any guide/link
Thanks
Custom ListView Adapters are often back by Collections, like List, ArrayList, etc. These lists may contain your custom objects, where the Adapter determines what to display based on the object properties. The lists can be managed/altered in typical fashions, like add() remove(), etc.
Do a Google search for "android custom list adapters" to learn more.
Here is an example of dynamically creating list rows with a "plus" buttons to add more rows. Should give you a good start: Dynamically add elements to a listView Android
You can always look at the source of the Android contacts app, as well.
i have to make a sectioned list in my application. For this i am using the following approach:
make a listview to contain the headers of each sections
the xml inflated in the getview of each item in the above list consists of a textview and a tablelayout
the custom adapter used to make the views for the above listview, i fill the textview with the header and add rows into the tablelayout until all the section is filled.
Naturally i maintain two arrays: 1) for the headers 2) for the section details(actually for this i use a hashmap with the section header index in its array as the key, this is my of identifying which header belongs to which section).
for some reason the above code is not working and the data is being repeated in different sections...eg. the second section contains data of the 1st and the 2nd section combined?
why is this happening?
Doesn't the idea mentioned above seem correct?
what is going wrong over here?
thank you in advance.
For hierarchical data structures you should rather use ExpandableListView/ExpandableListActivity then ListView (see this example for reference).
This way you'll use a BaseExpandableListAdapter to populate your list, which has two methods for the renderers:
getGroupView should be overridden
for the headers,
getChildView should be overridden
for the child items of the different
headers
If you don't need your list to be collapsable/expandable, you set all of your groups expanded, and disable collapsing as described here.
I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.