Wen
Wen

Reputation: 397

ListView with section header problem

Hi I'm adding section headers for my listView, there are few methods on the web. Since I'm using simpleCursorAdapter to manage my listView, I adopted phil bogle's method. http://thebogles.com/blog/2010/02/section-headers-for-android-listviews/

The error occurred at this part of the code:

    listAdapter.setViewBinder( new SimpleCursorAdapter.ViewBinder()
    {
        public boolean setViewValue(View view, Cursor cursor, int
                                   columnIndex)
        {
              if (columnIndex == nDateIndex) {
                if (isHeaderVisible(cursor)) {
                    view.setVisibility(View.VISIBLE);
                     prevDate = dateString;
                    ((TextView) view).setText(dateString);
                } else {
                    ((TextView) view).setText("");
                    view.setVisibility(View.GONE);
                }
                return true;
            }
            return false;
        }
    });

This is the LogCat shows how they compare while I scroll up the list:

09-06 11:06:43.460: INFO/System.out(563): Sun, 05 Sep 2010 11:41:53  (dateString)
09-06 11:06:43.460: INFO/System.out(563): Wed, 01 Sep 2010 21:58:06  (preDate)
09-06 11:06:43.460: INFO/System.out(563): 120.0                      (record)
09-06 11:06:43.820: INFO/System.out(563): Sun, 05 Sep 2010 17:47:26  (dateString)
09-06 11:06:43.820: INFO/System.out(563): Sun, 05 Sep 2010 11:41:53  (preDate)
09-06 11:06:43.820: INFO/System.out(563): 112.0                      (record)

The correct output should be:
Sun, 05 Sep 2010
112
120

But when I scroll up, the setViewValue calls 120 first and compare with preDate(show above), it becomes:
Sun, 05 Sep 2010
112
Sun, 05 Sep 2010
120

So how can I make the section headers fixed after creating the headers? Why setViewBinder been called while scrolling? Any suggestions on this? Thanks!

Upvotes: 2

Views: 5180

Answers (3)

Jarek Potiuk
Jarek Potiuk

Reputation: 20047

You might also take a look at very simple project here: http://code.google.com/p/android-section-list/. Inspiration came from iOS's section list (comes out of the box in iOS).

Upvotes: 3

See the ListView with section headers widget here, this may help you. It works similarly to the default Contacts application.

http://code.google.com/p/android-amazing-listview/

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006614

So how can I make the section headers fixed after creating the headers?

Have isHeaderVisible() work properly, I guess. Near as I can tell, that is where your problem lies.

Why setViewBinder been called while scrolling?

Because rows are recycled when they are scrolled.

Upvotes: 0

Related Questions