Skip to content
XeveAbout code and problems

Android DialogFragment that gives the user some list options to choose from

March 19, 2013 0 comments Article Uncategorized nspo
  • 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);
 }

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Calendar

March 2013
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
    Sep »

Archives

  • January 2021
  • May 2020
  • April 2020
  • July 2018
  • May 2018
  • April 2018
  • March 2018
  • September 2015
  • August 2015
  • June 2015
  • March 2015
  • February 2015
  • September 2014
  • March 2013

Categories

  • Uncategorized

Copyright Xeve 2021 | Theme by ThemeinProgress | Proudly powered by WordPress