Android listview groups - android

I have Activity for displaying search results. It extends ListActivity. I need to show search results in listview grouping several items. So, how do I add grouping to my listview?

You give your ListView a ListAdapter that knows how to do the grouping.
This could be a matter of overriding getViewTypeCount() and getItemViewType() in the adapter to describe which positions get which sorts of rows, plus modifying getView() (or newView() and bindView() for CursorAdapter) as needed.
Depending on what you are starting with, you might be able to use something like my MergeAdapter instead.

Related

Listening for a change in number of elements of a ListView backed by an ArrayAdapter

I have a ListView, which uses an ArrayAdapter. When the number of elements is changed, I'd like to doSomething(). This should include instances where a filter is applied.
What's the most elegant way of achieving this?

Android - ListView Adapter with items of two views

I'm new at Android programming and I'm stuck in a situation for two days that I can't seem to solve.
I've got a ListActivity that should be populated with two different views that has almost no simularity. Untill now I've used MergeAdapter and it worked fine but my outcome was like this:
Adapter1,Adapter2. and I didn't have the ability to change the order of the items coming to the list.
My question is: is it possible to create an adapter that is holding items of two views and not adapters of two views so I'll have my items sorted by the way I input them?
For simplicity sake, I got an ArrayList of those two items and each has "int position" so I'll be able to insert them into the list sorted by position.
EDIT: I've tried extending BaseAdapter but once again I need two adapters as I've read online but if I do so, I won't be able to control the place of an item on the list.
I hope I'm more clear this time.
Welcoming any response.
Thank you.
You can subclass BaseAdapter and utilize the following methods:
getItemViewType(int position)
getViewTypeCount()
getViewTypeCount() should return the number of different row types your adapter supports. getItemViewType() is where you implement the "decision" of which view type a particular row should have. If, for example, getViewTypeCount() returns 3, then getItemViewType() method should return 0, 1, or 2.
You can use this inside of getView to inflate/create a different layout for different row types.
EDIT:
Since it's a custom adapter, you can implement it in whatever way makes sense for your data. If you can create a data structure that works for what you want, then just make the adapter able to work with that. In the worst case, you might just have an array (or list) of Objects and have to use instanceof to do the decision work.

Android - Does SimpleCursorAdapter allow multiple layouts like BaseAdapter?

I know you can create a custom Adapter extending BaseAdapter and create various layouts which can be inflated depending on which row the AdapterView is at..
But is there any way to get a simple amount of customization with a SimpleCursorAdapter?
Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.
Will SimpleCursorAdapter do? Or are there any elegant solutions for this?
Cheers
But is there any way to get a simple amount of customization with a SimpleCursorAdapter?
Just like BaseAdapter, you can extend CursorAdapter or SimpleCursorAdapter to do your customization.
Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.
If you are only alternating a minor thing like row color, you can simply override bindView() and check if(cursor.getPosition() % 2 == 0) (or == 1) to set the appropriate background color.
If you are using different types of or numbers of Views in each layout, you need to override getViewTypeCount() and getItemViewType(). Then use getItemViewType() in newView() to load the appropriate layout and in bindView() to display the appropriate data.

Confused when to use Adapters and which one?

I have read a little bit about ListAdapter - ArrayAdapter - BaseAdapter - CursorAdapter. I don’t understand there usage in true sense.
I have scenario, in which I am showing word in a TextView and then there are 4 radio button options and user will select one.
I am confused if I can use any adapter functionality. Like when the word on the top is move to next word. Meaning selection shown as radio button options updates automatically because of binding.
In a nut shell i am looking for something like auto binding in .NET.
BaseAdapter is the most basic Adapter of ListView. All remaining adapters extend from BaseAdapter.
If you are confused which adapter is suitable for you scenario, let choose BaseAdapter first.
ArrayAdapter can be used to link say a list of items with an array. The ArrayAdapter works between an your array data and a list item layout to populate a list.
a CursorAdapter can work in a similar way but can link a database query result set (in a cursor) to a list by populating list items.
They are both subclasses of ListAdapter
If data changes in either the database tables or Array both the Array and Cursor Adapters can be refreshed via notifyDataSetChanged()

How to generate a ListView with headers above some sections?

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.

Categories

Resources