package com.marakana;

import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class TimelineAdapter extends SimpleCursorAdapter { // <1>
  static final String[] from = { DbHelper.C_CREATED_AT, DbHelper.C_USER,
      DbHelper.C_TEXT }; // <1>
  static final int[] to = { R.id.textCreatedAt, R.id.textUser, R.id.textText }; // <2>

  // Constructor
  public TimelineAdapter(Context context, Cursor c) { // <3>
    super(context, R.layout.row, c, from, to);
  }

  // This is where the actual binding of a cursor to view happens
  @Override
  public void bindView(View row, Context context, Cursor cursor) { // <4>
    super.bindView(row, context, cursor);

    // Manually bind created at timestamp to its view
    long timestamp = cursor.getLong(cursor
        .getColumnIndex(DbHelper.C_CREATED_AT)); // <5>
    TextView textCreatedAt = (TextView) row.findViewById(R.id.textCreatedAt); // <6>
    textCreatedAt.setText(DateUtils.getRelativeTimeSpanString(timestamp)); // <7>
  }

}
