Rentals.java :  » App » mycollectionpro » com » app » my_collection » Android Open Source

Android Open Source » App » mycollectionpro 
mycollectionpro » com » app » my_collection » Rentals.java
package com.app.my_collection;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Rentals extends ListActivity {

  /****************
   * CLASS FIELDS *
   ****************/
  MoviesDbAdapter mDbHelper;
  long rentalId = 0;
  String filterExpr = "";
  private static final int INSERT_ID = Menu.FIRST;
  private MenuItem insertMenuItem;
  private static final int EDIT_ID = Menu.FIRST + 1;
  private MenuItem editMenuItem;
  private static final int DELETE_ID = Menu.FIRST + 2;
  private MenuItem deleteMenuItem;
  private static final int CONTEXT_EDIT_ID = Menu.FIRST + 3;
  private static final int CONTEXT_DELETE_ID = Menu.FIRST + 4;
  private static final int ACTIVITY_CREATE = 0;
  private static final int ACTIVITY_EDIT = 1;
  
  /** 
   * Called when the activity is first created. 
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rentals);
    setTitle(R.string.title_rentals);
    mDbHelper = new MoviesDbAdapter(this);
    mDbHelper.open();
    registerForContextMenu(getListView());
    
    /**
     * In order to allow operations from the keyboard, we needed to set
     * the movieId when it was selected.  When nothing is selected, we
     * need to clear the selection as well
     */
    getListView().setOnItemSelectedListener(new OnItemSelectedListener() {
      public void onItemSelected(AdapterView<?> parent, View v,
          int possition, long id) {
        v.setSelected(true);
        rentalId = id;
        if(editMenuItem != null) {
          editMenuItem.setEnabled(true);
        }
        if(deleteMenuItem != null) {
          deleteMenuItem.setEnabled(true);
        }
      }

      public void onNothingSelected(AdapterView<?> parent) {
        parent.setSelection(-1);
        if(editMenuItem != null) {
          editMenuItem.setEnabled(false);
        }
        if(deleteMenuItem != null) {
          deleteMenuItem.setEnabled(false);
        }
      }
    });
    fillData();
  }//End of public void method onCreate(Bundle)

  /**
   * This method fetches the list of rentals from the database and
   * binds it to the ListView
   */
  private void fillData() {
    
    // Get all of the rows from the database and create the item list
    Cursor mRentalsCursor;
    mRentalsCursor = mDbHelper.fetchAllRentals();
    startManagingCursor(mRentalsCursor);
    
    // Now create an array adapter and set it to display using our row
        RentalsPanelAdapter rentalsAdapter = new RentalsPanelAdapter(this, mRentalsCursor);
        setListAdapter(rentalsAdapter);

    
  }//End of private void method fillData()
  
  /**
   * Sets up the menus and adds a nice menu icon.
   */
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    insertMenuItem = menu.add(0, INSERT_ID, 0, R.string.menu_add);
    insertMenuItem.setIcon(android.R.drawable.ic_menu_add);
    editMenuItem = menu.add(0, EDIT_ID, 0, R.string.menu_edit);
    editMenuItem.setIcon(android.R.drawable.ic_menu_edit);
    deleteMenuItem = menu.add(0, DELETE_ID, 0, R.string.menu_remove);
    deleteMenuItem.setIcon(android.R.drawable.ic_menu_delete);
    return true;
  }
  
  /**
   * Menu event handler.  We need to launch a new movie, edit an 
   * existing movie, delete a movie, or launch the movie import action
   */
  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case INSERT_ID:
      createRental();
      return true;
    case EDIT_ID:
      editRental();
      return true;
    case DELETE_ID:
      deleteRental();
      return true;
    }
    return super.onMenuItemSelected(featureId, item);
  }//End of public boolean onMenuItemSelected(in, MenuItem)
  
  /**
   * Creates the context menu
   */
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    menu.add(0, CONTEXT_EDIT_ID, 0, R.string.menu_edit);
    menu.add(0, CONTEXT_DELETE_ID, 0, R.string.menu_remove);
    
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
        android.util.Log.e("ItemId Retreival", "bad menuInfo", e);
        return;
    }
    rentalId  = getListAdapter().getItemId(info.position);
  }//End of method public void onCreateContextMenu(ContextMenu, view, ContextMenuInfo)
  
  /**
   * Handles the context menu item selection
   */
  public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
      case CONTEXT_EDIT_ID:
        editRental();
        return true;
      case CONTEXT_DELETE_ID:
        deleteRental();
        return true;
    }
    return super.onContextItemSelected(item);
  }//End of method public boolean onContextItemSelected(MenuItem)
  
  /**
   * Launches the EditRental activity and passes NO id
   */
  private void createRental() {
    Intent i = new Intent(this, EditRental.class);
    startActivityForResult(i, ACTIVITY_CREATE);
  }//End of method private void createRental()
  
  /**
   * Launches the EditRental activity and passes the rentalId
   */
  private void editRental() {
    Intent i = new Intent(this, EditRental.class);
    i.putExtra(MoviesDbAdapter.KEY_RENTAL_ID, rentalId);
    startActivityForResult(i, ACTIVITY_EDIT);
  }//End of method private void editRental()
  
  /**
   * Deletes the rental by setting the DateIn to todays date
   */
  private void deleteRental() {
    NotificationHelper.showYesNoAlert(this, "Confirm Deletion"
      , "Are you sure you want to delete this item?"
      , new OnClickListener() {
  
        @Override
        public void onClick(DialogInterface dialog, int which) {
          mDbHelper.deleteRental(rentalId);
          fillData();
        }
      
      }, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          //Do nothing
        }
      }
    );
  }//End of method private void deleteRental()
  
  /**
   * Handles the delete and enter key being pressed
   */
  @Override 
  public boolean onKeyUp(int keyCode, android.view.KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_DEL ) {
      deleteRental();
    }
    if(keyCode == KeyEvent.KEYCODE_ENTER ) {
      editRental();
    }
    return super.onKeyUp(keyCode, event);
  }//End of method public boolean onKeyUp(int, KeyEvent)
  
  
  /**
   * Handles when the activity calls back after completion
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode,
      Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode != RESULT_CANCELED) {
      fillData();
    }
  }//End of method protected void onActivityResult(int, int)
  
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    setSelection(position);
    rentalId = id;
    editRental();
  }//End of method protected void onListItemClick(ListView, View, int, long)
}//End of class Rentals
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.