Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Create a Dialog Fragment (can't store selections in boolean array)

$
0
0
Ok, I have an AlertDialog that creates a small dialog box allowing the user to select toppings and display the selections in a Toast. I need it to be able to store the selections in a boolean array named save, and when the user makes additional selections and hits cancel, it overwrites those selections with those the user originally made(If the user selected Bacon and Tomato for example, and hit OK, those would be stored. Then when the user goes back in, Bacon and Tomato are still selected, and the user selects Lettuce and hits cancel. The next time they open that dialog, only Bacon and Tomato would be selected, as if Lettuce was never selected.




// 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){
					mSelectedItems.add(which);
					choice[which]=true;
				}
				
				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
		


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>