CWAC MergeAdapter: Stitching Together Rows
MergeAdapter accepts a mix of
presents them as one contiguous whole to whatever
it is poured into. This is good for cases where you have
multiple data sources, or if you have a handful of ordinary
Views to mix in with lists of data, or the like.
Simply create a
MergeAdapter and call
addViews() (latter accepting a
then attach your adapter to the
ListView. You can also
MergeAdapter to override
isEnabled(), so you can
control which positions are and are not enabled.
There is also
MergeSpinnerAdapter for use with
This is packaged as an Android library project, though a simple JAR is also available from the Downloads section of this GitHub repository.
You can use
MergeAdapter directly or subclass it. The
latter is needed to support controlling which rows are/are
There is only one, no-argument constructor at this time. Just
new MergeAdapter() or
new MergeSpinnerAdapter() and
you are on your way!
You have three methods for defining what goes into the
MergeAdapter. You can call
addAdapter() to have all of that
adapter's rows appear in the combined roster. You can call
addView() to add a single
View as a row. You can also call
addViews() to add a
View objects to use as rows.
MergeSpinnerAdapter only supports
Each of these will appear in combined roster in the order they were added.
addViews() methods have a variant that
accepts a boolean 2nd parameter. Set this boolean to true if you
want the rows represented by these views to be enabled (i.e.,
selectable). The default is that they are disabled, for use
as if they were header rows. For adapters added via
the determination of whether or not rows are enabled is determined
by the underlying adapter.
Other Methods to Override
You are welcome to override other methods as well, since this
is just an
You must pour the contents into the
setListAdapter() to associate the
ListView. This limitation is required because Android
only calls getViewTypeCount() once, and adding more views or
adapters adds more view types.
Note, though, that you can modify the underlying adapters. So,
for example, if you add a
CursorAdapter to the
Cursor, the changes should be reflected
MergeAdapter to whatever
is connected to.
ListActivity supports a widget in its layout, with an
@android:id/empty, which will be displayed if the list is
empty. More specifically, it will be displayed if the
isEmpty(). The default implementation of
getCount() and compares it to
In many cases, this default will be fine. However, if you are
Views to the adapter (e.g., section headings),
getCount() will include these additional rows, and so
will never return
true. In this case, you may need to override
isEmpty() to implement your own business logic to determine
when the list is, indeed, "empty".
You should add properly-configured
CursorAdapter) to a
to have it work properly.
This project requires the CWAC SackOfViewsAdapter.
A copy of a compatible JAR can be found in the
libs/ directory of
the project, though you are welcome to try newer ones, or
ones that you have patched yourself.
This is version v0.3 of this module, meaning it is not completely scary now.
demo/ sub-project you will find
a sample activity that demonstrates the use of
Note that when you build the JAR via
ant jar, the sample
activity is not included, nor any resources -- only the
compiled classes for the actual library are put into the JAR.
The code in this project is licensed under the Apache Software License 2.0, per the terms of the included LICENSE file.
If you have questions regarding the use of this code, please post a question
on StackOverflow tagged with
android. Be sure to indicate
what CWAC module you are having issues with, and be sure to include source code
and stack traces if you are encountering crashes.
If you have encountered what is clearly a bug, please post an issue. Be certain to include complete steps for reproducing the issue.
Do not ask for help via Twitter.
getAdapter() method to return the
ListAdapter associated with a given position
v0.2.0: converted to Android library project, added enabled versions of
addViews(), correctly cascades data set changes from underlying adapters