1

我有一个DialogFragment弹出自定义铃声选择器。Ringtone但是,在选择一个并单击“确定”后,它不会正确。知道下面有什么问题吗?

对话框由此创建

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        rm = new RingtoneManager(getActivity());
        ringtones = rm.getCursor();

        int selected = -1;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.ringtone);
        builder.setSingleChoiceItems(ringtones,
                selected,
                ringtones.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX),
                mChoiceListener);
        builder.setPositiveButton("OK", this);
        builder.setNegativeButton("Cancel", this);
        return builder.create();

    }

    private DialogInterface.OnClickListener mChoiceListener =
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int position) {
                ringtones.moveToPosition(position);
            }

    };

当 OK 按钮被选中时,它被捕获

public void onClick(DialogInterface dialog, int which) {
        Log.d("DayPickerFragment", "onClick");
        switch(which) {
            case Dialog.BUTTON_NEGATIVE:
                dialog.cancel();
                break;

            case Dialog.BUTTON_POSITIVE:
                Uri uri = rm.getRingtoneUri(ringtones.getPosition());
                Ringtone r = RingtoneManager.getRingtone(getActivity(), uri);

                /*** etc ***/

                /*** returned ringtone doesn't match what was selected ***/
                dialog.dismiss();
                break;
        }
    }

是不是铃声光标rm.getRingtoneUri(position)对应的方法不对?如果是这样,您如何获得所选项目的真实性?Uri

任何帮助表示赞赏。

4

1 回答 1

0

不知道为什么它不起作用。我猜我不了解该Cursor对象的某些内容。

然而,这就是最终的结果,使用私有变量来移动它

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        rm = new RingtoneManager(getActivity());
        ringtones = rm.getCursor();

        int selected = -1;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.ringtone);
        builder.setSingleChoiceItems(ringtones,
                selected,
                ringtones.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX),
                this);
        builder.setPositiveButton("OK", this);
        builder.setNegativeButton("Cancel", this);
        return builder.create();
    }

public void onClick(DialogInterface dialog, int position) {
        switch(position) {
            case Dialog.BUTTON_NEGATIVE:
                mp.reset();
                mp.release();
                dialog.cancel();
                break;

            case Dialog.BUTTON_POSITIVE:
                mp.reset();
                mp.release();

                Uri uri2 = rm.getRingtoneUri(mPosition);
                Ringtone r = RingtoneManager.getRingtone(getActivity(), uri2);
                /*** Use ringtone as needed ***/
                dialog.dismiss();
                break;

            default: // dialog click
                ringtones.moveToPosition(position);
                mPosition = position;

                Uri uri = rm.getRingtoneUri(mPosition);
                mp.reset();
                try {
                    mp.setDataSource(getActivity(), uri);
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

我正在做一些额外的事情,比如传入一个铃声并将其设置为已选择(如果找到),但这是基本思想。

于 2012-06-09T02:10:03.777 回答