如何坚持AlertDialog选择的选项从ItemizedOverlay方法的onTap催生 [英] How to persist options selected in AlertDialog spawned from ItemizedOverlay onTap method

查看:202
本文介绍了如何坚持AlertDialog选择的选项从ItemizedOverlay方法的onTap催生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加的列表选择一个 AlertDialog Android官方文档暗示节省了用户的preferences用的数据存储技术的。这些示例假设 AlertDialog 已经在活动中催生类。

在我来说,我已经创建了一个扩展 ItemizedOverlay A类。这个类覆盖中的onTap 方法,并使用 AlertDialog 来提示用户做出<一个href=\"http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setMultiChoiceItems\"相对=nofollow>多选择选择。我想捕捉并持续为每个选择 OverlayItem 他们轻触。

下面code是方法的onTap我写的。它的功能是写,但还没有做什么,我倒是希望。我想捕捉和持续由用户作出的每个选择待以后使用。我怎么做?以这种方式使用 AlertDialog 一个好主意?有没有更好的选择?

 保护布尔中的onTap(INT指数)
{
    OverlayItem项目= _overlays.get(指数);
    最终的CharSequence [] =项目{无线网络连接,蓝牙};
    最终布尔[] =检查{假的,假};    AlertDialog.Builder建设者=新AlertDialog.Builder(_context);
    builder.setTitle(item.getTitle());
    builder.setMultiChoiceItems(物品,托运,新
        DialogInterface.OnMultiChoiceClickListener()
        {
          @覆盖
          公共无效的onClick(DialogInterface对话框,INT项目,
              布尔器isChecked)
              {
                  //现在只是表明用户触摸一个选项
                  Toast.makeText(_context,项目[项目]
                    Toast.LENGTH_SHORT).show();
              }
        });
    builder.setPositiveButton(好,新DialogInterface.OnClickListener()
    {
        @覆盖
        公共无效的onClick(DialogInterface对话框,INT ID)
        {
            //我应该调查什么在这里检查?
            dialog.dismiss();
        }
    });    builder.setNegativeButton(取消,新DialogInterface.OnClickListener()
    {
        @覆盖
        公共无效的onClick(DialogInterface对话框,INT ID)
        {
            dialog.cancel();
        }
    });    AlertDialog警报= builder.create();
    alert.show();
    返回true;
}


解决方案

那么你可以创建一个的HashMap&LT; OverLayItem,列表与LT;弦乐&GT;&GT; 的HashMap&LT; OverLayItem,列表与LT;布尔&GT;&GT; 来存储每个项目的设定,如果你只需要访问它在内存中,或者如果您需要更持久的商店,你可以使用preferences或数据库或在您所提供的链接解释的其他手段。然后你会更新包含HashMap或数据库中的积极的onClick()

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.

The below code is the onTap method I've written. It functions as written but doesn't yet do what I'd hope. I'd like to capture and persist each selection made by the user to be used later. How do I do that? Is using an AlertDialog in this manner a good idea? Are there better options?

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;
}

解决方案

Well you could create a HashMap<OverLayItem, List<String>> or HashMap<OverLayItem, List<Boolean>> to store the setting for each item if you only need to access it in memory, or if you need more persistent store you can use either the preferences or a database or other means as explained in the link you provided. Then you'd update the hashmaps or your database in the positive onClick().

这篇关于如何坚持AlertDialog选择的选项从ItemizedOverlay方法的onTap催生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆