// Project: Lab6_Lefelhocz
// Date: 2/18/2013
// Author: Joshua Lefelhocz
// Description: Demonstrates how to use AlertBuilder to create a simple dialog
package com.example.java2dialogfragmentex;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Toast;
public class ToppingsDialogFragment extends DialogFragment
{
private ArrayList<Object> mSelectedItems = new ArrayList<Object>(); // hint: use static for Lab6
private String msg = ""; // message to be displayed in Toast
private String[] arr; // array of toppings
static boolean[] choice = new boolean[3];
static boolean[] save = new boolean[3];
// hint: for lab 6 maybe use a static boolean array
@Override public Dialog onCreateDialog(Bundle savedInstanceState)
{
// create the Dialog builder object
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.pick_toppings);
// get the resource object and then copy the toppings resource array to a String array
Resources res = this.getResources();
arr = res.getStringArray(R.array.toppings);
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
builder.setMultiChoiceItems(R.array.toppings, save, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked){
choice[which]=true;
mSelectedItems.add(which);
}
else if (mSelectedItems.contains(which)){
mSelectedItems.remove(Integer.valueOf(which));
choice[which]=false;
}
}
});
// Set the positive action button
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
// build a string from the selected items and display them in a Toast
for(Object item : mSelectedItems)
{
msg += arr[Integer.parseInt(item.toString())] + " ";
}
Toast.makeText(getActivity(), "Selected: " + msg, Toast.LENGTH_LONG).show();
System.arraycopy(choice,0,save,0,3);
}
});
// negative action button
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(getActivity(), "Cancel Selected", Toast.LENGTH_LONG).show();
System.arraycopy(save,0,choice,0,3);
}
});
return builder.create();
} // end of onCreate
} // end of class