package com.example.fueltrack;

import android.app.Activity;
import android.app.ListFragment;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class FuelHistoryActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.file_view);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      finish();
      return;
    }

    if (savedInstanceState == null) {
      FuelHistoryFragment fragment = new FuelHistoryFragment();
      fragment.setArguments(getIntent().getExtras());
      getFragmentManager().beginTransaction()
          .add(android.R.id.content, fragment).commit();
    }
  }
  
  public static class FuelHistoryFragment extends ListFragment {
    /** private SimpleCursorAdapter sca; **/
    private Cursor cursor;
    private FuelDbAdapter dba;
    private FuelCursorAdapter mca;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      dba = new FuelDbAdapter(getActivity());
      dba.open();
      cursor = dba.getMileageList();
      getActivity().startManagingCursor(cursor);
      
      /** To use SimpleCursorAdapter:
      String[] columns = new String[] {
      
          MileageDBAdapter.KEY_DATE,
          MileageDBAdapter.KEY_DISTANCE,
          MileageDBAdapter.KEY_FUEL,
          MileageDBAdapter.KEY_COST
      };
      int[] views = new int[] {
          R.id.dateView,
          R.id.distanceView,
          R.id.fuelView,
          R.id.costView
      };
      sca = new SimpleCursorAdapter(getActivity(), R.layout.mileage_list_item,
          cursor, columns, views);
      setListAdapter(sca);
      **/
      
      /** To use a custom CursorAdapter: **/
      getActivity().setContentView(R.layout.mileage_list);
      mca = new FuelCursorAdapter(getActivity(), cursor);
      setListAdapter(mca);
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
      String msg = "Position: " + position + " ID: " + id;
      Toast toast = Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG);
      toast.show();
    }

  }
}
