Back to project page MythTrack.
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.
/** * RemoveItemDialog presents a dialog for the user confirm the sale or removal of an item from the * hero's inventory/*from w w w .j av a2s . co 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 - RemoveItemDialog // //////////////////////////////////////////////////////////////////////////////////////////////////// public class RemoveItemDialog extends DialogFragment { // INTERFACES //////////////////////////////////////////////////////////////////////////////////// /** Interface for a listener for when an item is deleted from an inventory.*/ OnItemRemovedListener itemRemovedCallback; public interface OnItemRemovedListener { public void onItemRemoved(long inventoryID, 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 { itemRemovedCallback = (OnItemRemovedListener) activity; } catch(ClassCastException exception) { throw new ClassCastException(activity.toString() + " must implement onItemRemoved"); } } /** * 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 inventoryID = getArguments().getLong("inventory_id"); final boolean selling = getArguments().getBoolean("selling"); final String itemName = getArguments().getString("item_name"); final int itemValue = getArguments().getInt("item_value"); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // User chose to sell the item. if(selling) { builder.setTitle(R.string.inventorylist_sell_title); String formatMessage = getResources().getString(R.string.inventorylist_sell_message); String message = String.format(formatMessage, itemName, itemValue); builder.setMessage(message); // Set up the "Sell" button and its actions. builder.setPositiveButton(R.string.inventorylist_sell_positive_button, new DialogInterface.OnClickListener() { /** * Handle the "Remove" button being clicked. * @param dialogInterface The dialog being handled. * @param button Which button was clicked. */ @Override public void onClick(DialogInterface dialogInterface, int button) { itemRemovedCallback.onItemRemoved(inventoryID, itemValue); } }); // 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. } }); } // User chose to remove the item. else { // Set up the "Remove" button and its actions. builder.setTitle(R.string.inventorylist_remove_title); String formatMessage = getResources().getString(R.string.inventorylist_remove_message); String message = String.format(formatMessage, itemName); builder.setMessage(message); builder.setPositiveButton(R.string.inventorylist_remove_positive_button, new DialogInterface.OnClickListener() { /** * Handle the "Remove" button being clicked. * @param dialogInterface The dialog being handled. * @param button Which button was clicked. */ @Override public void onClick(DialogInterface dialogInterface, int button) { itemRemovedCallback.onItemRemoved(inventoryID, 0); } }); // 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(); } }