StickyListHeaders icon indicating copy to clipboard operation
StickyListHeaders copied to clipboard

Section header only getting set at the top (CursorAdapter implementation)

Open jinxk opened this issue 9 years ago • 6 comments

Problem - The section header is there and it works, but it is always at the top, so if a new list item comes on the screen that should have a different header - the header on the top gets changed instead of a new header getting placed in between

I am using this library with CursorAdapter (extends CursorAdapter implements StickyListHeadersAdapter) to section my notes into different periods (Today, Yesterday, Last Week, Earlier) Following is my implementation of the methods (let me know if you require anything else along with this)

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {

        HeaderViewHolder holder;
        if (convertView != null)
            holder = (HeaderViewHolder) convertView.getTag();
        else{
            convertView = mInflater.inflate(R.layout.notes_row_header, parent, false);
            holder = new HeaderViewHolder(convertView);
            convertView.setTag(holder);
        }

        String newDateString = getCursor().getString(getCursor().getColumnIndex(NotesEntry.COLUMN_LASTEDIT_DATETEXT));
        //get the new date and format it accordingly
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime newDate = formatter.parseDateTime(newDateString);
        int diffSinceMonday = UtilityFunctions.getDateDiff(newDate.withDayOfWeek(1));
        int diffDates = UtilityFunctions.getDateDiff(newDate);
        //set appropriate code
        if(diffDates == 0)
            dayDiff = HEAD_TODAY;
        else if(diffDates == 1)
            dayDiff = HEAD_YESTERDAY;
        else if(UtilityFunctions.isBetween(diffDates, 1, diffSinceMonday))
            dayDiff = HEAD_THIS_WEEK;
        else dayDiff  = HEAD_EARLIER;
        Log.v("IN_ getHeaderView", "ID IS "+ dayDiff + " " + newDateString);

        String headerText= "" + HEADER_TEXT[dayDiff];
        Log.v("IN_ getHeaderView", "HEADER IS "+ HEADER_TEXT[dayDiff] + " " + dayDiff);
        holder.headerText.setText(headerText);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {

        String newDateString = getCursor().getString(getCursor().getColumnIndex(NotesEntry.COLUMN_LASTEDIT_DATETEXT));
        //get the new date and format it accordingly
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime newDate = formatter.parseDateTime(newDateString);
        int diffSinceMonday = UtilityFunctions.getDateDiff(newDate.withDayOfWeek(1));
        int diffDates = UtilityFunctions.getDateDiff(newDate);
        //set appropriate code
        if(diffDates == 0)
            dayDiff = HEAD_TODAY;
        else if(diffDates == 1)
            dayDiff = HEAD_YESTERDAY;
        else if(UtilityFunctions.isBetween(diffDates, 1, diffSinceMonday))
            dayDiff = HEAD_THIS_WEEK;
        else dayDiff  = HEAD_EARLIER;
        Log.v("IN_ getHeaderId", "ID IS "+ dayDiff + " " + newDateString);
        return dayDiff;
    }

I wanted to know if I have to somehow initialize the header in newView or bindVIew implementations. I am also aware that having the date calculation part in getHeaderView and getHeaderId might be redundant, I have kept them at both places as I am not sure what would work (it definitely didn't work when it was just in getHeaderId).

If anyone has an example of cursor adapter implementation of this library I would appreciate if I got a chance to review it.

Please disregard the log messages - they were just for testing.

jinxk avatar Mar 29 '15 21:03 jinxk

I know this is very late but did you end up finding a solution? I'm facing the same problem when trying to use a CursorAdapter that implements StickyListHeadersAdapter

Edit: Just to clarify, using a BaseAdapter extension with a getView implementation is still working fine, it's just not exactly ideal to dump the cursor elements into an array list then feed that into a BaseAdapter rather than directly feed the Cursor into a CursorAdapter.

oseparovic avatar Jul 28 '15 01:07 oseparovic

Unfortunately no, I ended up using Recycler view in conjunction with some other sticky header lib.

On Tue, Jul 28, 2015 at 6:46 AM, oseparovic [email protected] wrote:

I know this is very late but did you end up finding a solution? I'm facing the same problem when trying to use a Cursor Adapter that implements StickyListHeadersAdapter

— Reply to this email directly or view it on GitHub https://github.com/emilsjolander/StickyListHeaders/issues/394#issuecomment-125399595 .

jinxk avatar Jul 28 '15 01:07 jinxk

Do you have any contact info I can reach you at? I'd like to discuss your solution if possible. There's no contact info available on your github profile.

oseparovic avatar Jul 28 '15 01:07 oseparovic

My email ID should now be visible on my profile.

On Tue, Jul 28, 2015 at 7:14 AM, oseparovic [email protected] wrote:

Do you have any contact info I can reach you at? I'd like to discuss your solution if possible. There's no contact info available on your github profile.

— Reply to this email directly or view it on GitHub https://github.com/emilsjolander/StickyListHeaders/issues/394#issuecomment-125402877 .

jinxk avatar Jul 28 '15 03:07 jinxk

Try to put this code in the first line of getHeaderView() and getHeaderId()

mCursor.moveToPosition(position);

gersonmdesouza avatar Jul 28 '15 14:07 gersonmdesouza

Can confirm that @gersonmendes answer solved it

ProfPh avatar Jun 27 '16 21:06 ProfPh