伙计们,在onCreateDialog里我是这样的:
case DIALOG_REVIEW: {
if (bundle.containsKey("POSITION")) {
final int position = bundle.getInt("POSITION");
ArrayList<String> alterNumbers = numbers.get(position);
final String[] phoneNums = new String[alterNumbers.size()];
for (int i = 0; i < alterNumbers.size(); i++) {
phoneNums[i] = alterNumbers.get(i);
}
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(names.get(position) + "'s number(s)");
dialog.setSingleChoiceItems(phoneNums, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// get selected item and close the dialog
String selectedNumber = phoneNums[which];
updateUserSelectedNumber(position , selectedNumber);
}
});
return dialog.create();
}
这是有效的和伟大的。
但要注意行
dialog.setSingleChoiceItems(phoneNums, 0,
new DialogInterface.OnClickListener() {
每次弹出对话框时,phoneNums都会发生变化。我已经覆盖了onPrepareDialog方法,但是我不知道如何给它赋新值。而且那里也没有setSingleChoiceItems。
下面是我的onPrepareDialog方法
case DIALOG_REVIEW: {
final int position = bundle.getInt("POSITION");
ArrayList<String> alterNumbers = numbers.get(position);
final String[] phoneNums = new String[alterNumbers.size()];
for (int i = 0; i < alterNumbers.size(); i++) {
phoneNums[i] = alterNumbers.get(i);
}
AlertDialog alertDialog = (AlertDialog) dialog;
alertDialog.setTitle(names.get(position) + "'s number(s)");
???
break;
}
解决方案是什么?提前谢谢各位。
发布于 2011-03-30 19:31:21
您需要使用AlertDialog类中的getListView方法。然后对返回的对象使用setItemChecked方法。示例:
alertDialog.getListView().setItemChecked(1, true);
发布于 2011-09-29 01:52:27
我刚刚遇到了同样的问题:
两种解决方案:
1/快速和肮脏
删除该对话框每次完成后,将不会调用=> onPrepareDialog,因此您不会遇到数据更新问题:
protected Dialog onCreateDialog(int id) {
...
case DIALOG_REVIEW: {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(names.get(position) + "'s number(s)");
dialog.setSingleChoiceItems(phoneNums, 0,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// get selected item and close the dialog
String selectedNumber = phoneNums[which];
updateUserSelectedNumber(position , selectedNumber);
removeDialog(DIALOG_REVIEW);
}
});
return dialog.create();
}
如果您愿意,您可以放置一个onDismissListener,然后在对话框中执行removeDialog。
2/相当漂亮的那个
在onPrepareDialog方法中,只需将对话框使用的旧ArrayAdapter替换为新的:
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_REVIEW:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.select_dialog_singlechoice, android.R.id.text1, phoneNums);
AlertDialog ad = (AlertDialog) dialog;
ad.getListView().setAdapter(adapter);
break;
default:
super.onPrepareDialog(id, dialog);
}
}
我使用与android相同的方法( froyo源代码的AlertController.java L.854)第一次填充对话框。
https://stackoverflow.com/questions/4811688
复制相似问题