Android Open Source - MythTrack Buy Item Dialog






From Project

Back to project page MythTrack.

License

The source code is released under:

MIT License

If you think the Android project MythTrack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * BuyItemDialog presents a dialog for the user confirm the purchase of an item.
 *//from w ww.  ja  va2  s  .c o  m
 * @author Nolan Jurgens
 */

package nolanjurgens.mythtrack.app;

// IMPORTS /////////////////////////////////////////////////////////////////////////////////////////
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import nolanjurgens.mythtrack.R;

////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS - BuyItemDialog                                                                          //
////////////////////////////////////////////////////////////////////////////////////////////////////
public class BuyItemDialog extends DialogFragment
{
  // INTERFACES ////////////////////////////////////////////////////////////////////////////////////

  /** Interface for a listener for when an item is purchased.*/
  OnItemBoughtListener itemBoughtCallback;
  public interface OnItemBoughtListener
  {
    public void onItemBought(long heroID, long itemID, int itemValue);
  }

  // METHODS ///////////////////////////////////////////////////////////////////////////////////////

  /**
   * Called when the fragment is attached to the activity.
   * @param activity The host activity.
   */
  @Override
  public void onAttach(Activity activity)
  {
    super.onAttach(activity);

    // Make sure the host activity implements the interface.
    try
    {
      itemBoughtCallback = (OnItemBoughtListener) activity;
    }
    catch(ClassCastException exception)
    {
      throw new ClassCastException(activity.toString() + " must implement onItemBought");
    }
  }


  /**
   * Called when the dialog is to be created.
   * @param arguments Arguments for the dialog.
   * @return The created dialog.
   */
  @Override
  public Dialog onCreateDialog(Bundle arguments)
  {
    final long heroID = getArguments().getLong("hero_id");
    final long itemID = getArguments().getLong("item_id");
    final String itemName = getArguments().getString("item_name");
    final int itemCost = getArguments().getInt("item_cost");

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.itemlist_buy_title);
    String formatMessage = getResources().getString(R.string.itemlist_buy_message);
    String message = String.format(formatMessage, itemName, itemCost);
    builder.setMessage(message);
    // Set up the "Buy" button and its actions.
    builder.setPositiveButton(R.string.itemlist_buy_positive_button,
      new DialogInterface.OnClickListener()
      {
        /**
         * Handle the "Buy" button being clicked.
         * @param dialogInterface The dialog being handled.
         * @param button Which button was clicked.
         */
        @Override
        public void onClick(DialogInterface dialogInterface, int button)
        {
          itemBoughtCallback.onItemBought(heroID, itemID, itemCost);
        }
      });
    // Set up the "Cancel" button and its actions.
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
    {
      /**
       * Handle the "Cancel" button being clicked.
       * @param dialogInterface The dialog being handled.
       * @param button Which button was clicked.
       */
      @Override
      public void onClick(DialogInterface dialogInterface, int button)
      {
        // Just exit the dialog.
      }
    });

    return builder.create();
  }

}




Java Source Code List

nolanjurgens.mythtrack.app.BuyItemDialog.java
nolanjurgens.mythtrack.app.CreateHeroDialog.java
nolanjurgens.mythtrack.app.DeleteHeroDialog.java
nolanjurgens.mythtrack.app.EquipmentInfoDialog.java
nolanjurgens.mythtrack.app.HeroDisplayFragment.java
nolanjurgens.mythtrack.app.HeroHelper.java
nolanjurgens.mythtrack.app.HeroList.java
nolanjurgens.mythtrack.app.HeroTrackerActivity.java
nolanjurgens.mythtrack.app.Hero.java
nolanjurgens.mythtrack.app.InventoryList.java
nolanjurgens.mythtrack.app.ItemHelper.java
nolanjurgens.mythtrack.app.ItemList.java
nolanjurgens.mythtrack.app.Item.java
nolanjurgens.mythtrack.app.MainActivity.java
nolanjurgens.mythtrack.app.RemoveItemDialog.java
nolanjurgens.mythtrack.app.SettingsActivity.java
nolanjurgens.mythtrack.app.SettingsFragment.java
nolanjurgens.mythtrack.app.StatInfoDialog.java
nolanjurgens.mythtrack.app.StatPickerDialog.java
nolanjurgens.mythtrack.app.TitleHelper.java
nolanjurgens.mythtrack.app.Title.java
nolanjurgens.mythtrack.provider.MythTrackBackup.java
nolanjurgens.mythtrack.provider.MythTrackContract.java
nolanjurgens.mythtrack.provider.MythTrackDatabase.java