Android DialogFragment that gives the user some list options to choose from
- DialogFragment that creates an AlertDialog with the title and options specified in the constructor
- Can store some a data object (useful if you want the user to select an action on a specific item)
- Is retained in case of an orientation change
- Works great with ActionBarSherlock
- FIXME: do not use custom constructor, but add data with .setArguments and a Bundle
package com.kopfgeldjaeger.fddbscanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockDialogFragment;
public class ChooserFragment extends SherlockDialogFragment implements
OnClickListener {
private ChooserFragmentInterface mInterface;
private String title;
private CharSequence[] options;
private Object dataObject;
public interface ChooserFragmentInterface {
/**
* Called when the user has made a choice
*
* @param choice
* Index of the option the user chose
* @param dataObject
* Data object passed to constructor
*/
public void onChooserFragmentResult(int choice, Object dataObject);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mInterface = (ChooserFragmentInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ChooserFragmentInterface");
}
}
/**
* @param title
* Dialog title
* @param options
* The options that the user can choose from
* @param dataObject
* Some data object you may want to store; can be null
*/
public ChooserFragment(String title, CharSequence[] options,
Object dataObject) {
this.title = title;
this.options = options;
this.dataObject = dataObject;
}
/**
* Default constructor
*/
public ChooserFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setItems(options, this);
AlertDialog alert = builder.create();
return alert;
}
@Override
public void onClick(DialogInterface dialog, int choice) {
mInterface.onChooserFragmentResult(choice, dataObject);
}
@Override
public void onDestroyView() {
// Work around bug:
// http://code.google.com/p/android/issues/detail?id=17423
Dialog dialog = getDialog();
if ((dialog != null) && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
}
// Example usage (in Activity(
// e.g. on LongClick on ListView item
CharSequence[] options = {"Choice 0", "Choice 1"};
ChooserFragment chooser = new ChooserFragment("Choose action", options, null);
chooser.show(getSupportFragmentManager(),
"chooserFragment");
//[...]
@Override
public void onChooserFragmentResult(int choice, Object dataObject) {
Toast.makeText(this, "User chose option No. "+choice, Toast.LENGTH_SHORT);
}
Recent Posts
Recent Comments
- 1win_qaSt on NMPC with CasADi and Python – Part 1: ODE and steady state
- Andreas on Shrinking a QNAP Virtualization Station disk image
- Arjun on Fix for Latex4CorelDraw with CorelDraw 2017
- Nigel De Gillern on No wired (LAN) connection in Linux, although everything looks right
- Harald H on Automatically reboot TP-Link router WDR4300 / N750 with bash script
Leave a Reply