package com.example.checkregister;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CheckRegisterHistoryActivity extends ListActivity {
  //private SimpleCursorAdapter sca;
  private Cursor cursor;
  private CheckRegisterDBAdapter dba;
  private Context context;
  private CheckRegisterCursorAdapter mca;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = (Context) this;
    dba = new CheckRegisterDBAdapter(this);
    cursor = dba.getCheckList();
    startManagingCursor(cursor);

    /** To use SimpleCursorAdapter:
    String[] columns = new String[] {    
      CheckRegisterDBAdapter.KEY_CHECKNUM,
      CheckRegisterDBAdapter.KEY_DATE,
      CheckRegisterDBAdapter.KEY_PAYEE,
      CheckRegisterDBAdapter.KEY_AMOUNT,
      CheckRegisterDBAdapter.KEY_BALANCE,
      CheckRegisterDBAdapter.KEY_MEMO,
    };
    int[] views = new int[] {
      R.id.checkNumView,
      R.id.dateView,
      R.id.payeeView,
      R.id.amountView,
      R.id.balanceView,
      R.id.memoView
    };
    sca = new SimpleCursorAdapter(context, R.layout.register_list_item,
                                  cursor, columns, views);
    setListAdapter(sca);
    **/

    /** To use a custom CursorAdapter: **/
    setContentView(R.layout.check_register_list);
    mca = new CheckRegisterCursorAdapter(context, cursor);
    setListAdapter(mca);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    dba.close();
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    String msg = "Position: " + position + " ID: " + id;
    Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    toast.show();
  }

}
