Reputation: 2517
I'm having a ListView
which displays a list of items from database. So I am using SimpleCursorAdapter
.
if (mDiscussionsCursor != null && mDiscussionsCursor.getCount() > 0) {
mAdapter = new SimpleCursorAdapter(mContext, R.layout.home_discussion_row, mDiscussionsCursor, FROM, TO, 0);
mAdapter.setViewBinder(VIEW_BINDER);
setAdapter(mAdapter);
setOnItemClickListener(this);
}
These items are chronologically ordered. I would like to put a separation line between every day, i.e., display all items from May 1, 2013, then a title 'May 2, 2013' followed by all items of that day etc.
I assume I can take my Cursor
and copy it to Array with special cells representing the titles for the days and then use the method presented here: http://android.amberfog.com/?p=296
However, this seems very wasteful practice to me in both space and time, and I am wondering maybe you can think of something more clever. Thanks in advance.
Upvotes: 1
Views: 77
Reputation: 6202
You can try sub-classing the SimpleCursorAdapter and overriding its associated members from the tutorial
getItemViewType
getViewTypeCount
getView
If you have a look at the inheritance hierarchies of SimpleCusrsorAdapter and ListAdapter (ListView adapter), you'll see that they are both subclasses of BaseAdapter, and is therefore why this is possible.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html http://developer.android.com/reference/android/widget/ListAdapter.html http://developer.android.com/reference/android/widget/BaseAdapter.html
EDIT
It will probably not be as cut and dry as simply subclassing and overriding. You may also need to incorporate some of the code in the CursorAdapter parent class into your overrides, and
bypass calling super.method().
For example, here is CursorAdapter.java's getView override, you may need to
explicitly call bindView()
(ref: http://androidxref.com/4.0.3_r1/xref/frameworks/base/core/java/android/widget/CursorAdapter.java
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
To use the xref source code repository, go to
select your api version, and then in
"Full Search"
type
ObjectName.java
and in the list to the right
"In Projects"
scroll down and select
"frameworks"
and it search (I find this the most direct and effective way to get what ya want).
Upvotes: 1