I have setup a SimpleCursorAdapter on my view and it works fine when I'm fetching the data from my database. The query is run using a ORDER BY date DESC-clause. All rows are fetched and displayed nicely in my ListView, and I'm using adapter.setViewBinder() to add some logic to the items (adding a thumbnail).
The code is like this:
// Setup the adapter
adapter = new SimpleCursorAdapter(this, R.layout.rowitem, cursor, FROM, TO);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
// Only taylor the ImageView in the adapter-list, skip everything else.
if (view.getId() != R.id.imageIt开发者_如何学Pythonem)
return false;
// here goes the logic, but it's cut away due to its size
}
return true;
}
});
Now I want to add some more logic: I want to add a small separator bar containing the date if the date is changed between two items, like this:
=== DATE1 ===============
Item 1
Item 2
=== DATE2 ===============
Item 3
=== DATE3 ===============
Item 4
Item 5
Item 6
=== DATE4 ===============
Item 7
and so on. However, I'm not sure how to insert a new View-element inside of an adapter and I cannot seem to find an answer on developer.android.com.
How can I accomplish this?
You can also look at a bit more robust solution which I developed: http://code.google.com/p/android-section-list/. It does even a bit more than you need (albeit nicer) - it works in the way that it adds a special adapter wrapping the original one and injecting section headers (in your case date headers) when needed. The headers remain sticky at top of the list (similarly as in iphone section lists) - so that you can always see which is the section you are in.....
You can use the section list as library - You would just have to change your adapter to return SectionListItem (and putting date into section) and then add it to SectionListView - that would do the job basically....
-- Edit --
Okay here is an easy solution for this problem:
Custom ListView with Date as SectionHeader (Used custom SimpleCursorAdapter)
--- old post --- I couldn't comment on your first Post but I wanted to kindly ask you if you could explain in a little more detail how you managed to get this list with date headers. You would have to iterate over all the curser-rows or is there a better way provided by the framework?
I have quite a few items and if I iterate with while(listData.moveToNext()){
it takes to long. (I also have to calculate from a unixtime stamp to a Calendar, to a Date, to a month-Int).
精彩评论