我有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);
}
};
选择确定按钮后,它将被
捕获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
?
感谢任何帮助。
答案 0 :(得分: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;
}
}
我正在做一些额外的事情,例如传入铃声并将其设置为已选中但如果已找到,但这是基本想法。