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:
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 TextView textName;
public TextView textCats;
public VisitorViewHolder(View v) {
super(v);
textName = (TextView) v.findViewById(R.id.item_visitor_name);
textCats = (TextView) v.findViewById(R.id.item_visitor_category);
}
}
public static class HeaderViewHolder extends RecyclerView.ViewHolder {
public TextView textName;
public TextView textCats;
public HeaderViewHolder(View v) {
super(v);
textName = (TextView) v.findViewById(R.id.item_visitor_name);
textCats = (TextView) v.findViewById(R.id.item_visitor_category);
}
}
private class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
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);
if(position == -1) return HEADER_LAYOUT;
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);
return 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 = null;
int position = cursor.getPosition();
int type = getItemViewType(position);
RecyclerView.ViewHolder viewHolder = null;
switch (type) {
case HEADER_LAYOUT:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_day, parent, false);
viewHolder = new HeaderViewHolder(v);
break;
case NO_HEADER_LAYOUT:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_visitor, parent, false);
viewHolder = new VisitorViewHolder(v);
break;
}
assert v != null;
v.setTag(viewHolder);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final int viewType = getItemViewType(cursor.getPosition());
switch (viewType) {
case HEADER_LAYOUT:
HeaderViewHolder holder = (HeaderViewHolder) view.getTag();
holder.textName.setText(DateFormat.getDateTimeInstance().format(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000)));
holder.textCats.setText(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_FIRST_NAME)));
break;
case NO_HEADER_LAYOUT:
VisitorViewHolder holder0 = (VisitorViewHolder) view.getTag();
holder0.textName.setText(DateFormat.getDateTimeInstance().format(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_ENTER))) * 1000)));
holder0.textCats.setText(cursor.getString(cursor.getColumnIndex(DatabaseConst.VisitorEntry.COLUMN_NAME_FIRST_NAME)));
break;
}
}
}
@Override
public int getItemCount() {
if(mCursorAdapter == null) return 0;
return mCursorAdapter.getCount();
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mCursorAdapter.getCursor().moveToPosition(position);
mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
RecyclerView.ViewHolder viewHolder = null;
switch (viewType){
case HEADER_LAYOUT:
viewHolder = new HeaderViewHolder(v);
break;
case NO_HEADER_LAYOUT:
viewHolder = new VisitorViewHolder(v);
break;
}
return viewHolder;
}
}
This is the actual wrong result:
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?