Save options selected in AlertDialog spawned from ItemizedOverlay onTap method

Posted by ahsteele on Stack Overflow See other posts from Stack Overflow or by ahsteele
Published on 2010-06-07T19:54:36Z Indexed on 2010/06/07 20:42 UTC
Read the original article Hit count: 474

In the description of how to add a list of options to an AlertDialog the official Android documentation alludes to saving a users preferences with one of the "data storage techniques." The examples assume the AlertDialog has been spawned within an Activity class.

In my case I've created a class that extends ItemizedOverlay. This class overrides the onTap method and uses an AlertDialog to prompt the user to make a multi-choice selection. I would like to capture and persist the selections for each OverlayItem they tap on. That said I am unsure if utilizing an AlertDialog in this manner is the right approach and open to other suggestions.

protected boolean onTap(int index)
{
    OverlayItem item = _overlays.get(index);
    final CharSequence[] items = { "WiFi", "BlueTooth" };
    final boolean[] checked = { false, false };

    AlertDialog.Builder builder = new AlertDialog.Builder(_context);
    builder.setTitle(item.getTitle());
    builder.setMultiChoiceItems(items, checked, new
        DialogInterface.OnMultiChoiceClickListener()
        {
          @Override
          public void onClick(DialogInterface dialog, int item,
              boolean isChecked)
              {
                  // for now just show that the user touched an option
                  Toast.makeText(_context, items[item],
                    Toast.LENGTH_SHORT).show();
              }
        });
    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int id)
        {
            // should I be examining what was checked here?
            dialog.dismiss();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int id)
        {
            dialog.cancel();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    return true;
}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk