خرید بک لینک

Vote count: 0

I need to display a sort of visitor logs in a RecyclerView and the data to display comes from a SQLiteDatabase DESC-ordered by DateTime (column enter_time, UNIX value). I have already implemented a simple CursorAdapter and I'm able to display data in the RecyclerView.

Next step is to make different views so that the first entry of every day uses a layout with an additional header with the date:

enter image description here

Inside the CursorAdapter I implemented getItemViewType(int) method where I need to add my logic selecting the right item type.

The logic is simple:

  • If next item in Cursor has same date as the current, then select the simple layout
  • If next item in Cursor has different date as the current, then select the layout with header

Here comes the problem: I need to check next item in the Cursor of the CursorAdapter but the displayed result is wrong and I can't tell why. Code looks correct to me but layout are assigned to items randomly.

public static class VisitorsRecyclerAdapter extends RecyclerView.Adapter<VisitorsRecyclerAdapter.VisitorViewHolder> {

    CustomCursorAdapter mCursorAdapter;
    Context mContext;
    private static final int NO_HEADER_LAYOUT = 1;
    private static final int HEADER_LAYOUT = 0;

    public VisitorsRecyclerAdapter(Context context, Cursor c) {
        mContext = context;
        mCursorAdapter = new CustomCursorAdapter(mContext, c, 0);
    }

    public static class VisitorViewHolder extends RecyclerView.ViewHolder {

        public VisitorViewHolder(View v) {
            super(v);
            ...
        }
    }

    private class CustomCursorAdapter extends CursorAdapter {

        public CustomCursorAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);
        }

        @Override
        public int getViewTypeCount() {
            retu 2;
        }

        @Override
        public int getItemViewType(int position) {
            if(position == -1) retu HEADER_LAYOUT;

            Cursor cursor = (Cursor) mCursorAdapter.getItem(position);
            cursor.moveToNext();

            // date of current item
            Date date0 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);

            cursor = (Cursor) mCursorAdapter.getItem(position - 1);
            cursor.moveToNext();

            // date of item that temporary comes after
            Date date1 = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000);

            SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy", Locale.ENGLISH);

            retu format.format(date0).equals(format.format(date1)) ? NO_HEADER_LAYOUT : HEADER_LAYOUT;
        }

        @Override
        public View newView(final Context context, Cursor cursor, ViewGroup parent) {
            View v;
            if(getItemViewType(cursor.getPosition()) == NO_HEADER_LAYOUT)
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_visitor, parent, false);
            else
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_day, parent, false);

            final VisitorViewHolder holder = new VisitorViewHolder(v);
            v.setTag(holder);
            retu v;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
                ...
        }

    }

    @Override
    public int getItemCount() {
        if(mCursorAdapter == null) retu 0;

        retu mCursorAdapter.getCount();
    }

    @Override
    public void onBindViewHolder(VisitorViewHolder holder, int position) {
        mCursorAdapter.getCursor().moveToPosition(position);
        mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
    }

    @Override
    public VisitorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
        final VisitorViewHolder holder = new VisitorViewHolder(v);

        retu holder;
    }
}

This is the actual wrong result:

enter image description here

I looked over SO, but I can't find similar scenario where it's needed to recover data from adjacent items (I'm not event sure is this the problem). What would you suggest to fix this issue?

asked 4 mins ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 2 ارديبهشت 1395 ساعت: 0:12

صفحه بندی