Var*_*ian 7 android dialog android-alertdialog android-activity
伙计们,在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();
}
Run Code Online (Sandbox Code Playgroud)
这是有效的.
但要注意线
dialog.setSingleChoiceItems(phoneNums, 0,
new DialogInterface.OnClickListener() {
Run Code Online (Sandbox Code Playgroud)
假设每次弹出对话框时都会更改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;
}
Run Code Online (Sandbox Code Playgroud)
解决办法是什么?先谢谢你们.
And*_*Duś 10
您需要使用AlertDialog类中的getListView方法.然后对返回的对象使用setItemChecked方法.例:
alertDialog.getListView().setItemChecked(1, true);
我刚遇到同样的问题:
两种解决方案
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();
}
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以放置一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的方法与android使用的方法(froyo源代码的AlertController.java L. 854)相同,第一次填充对话框.
| 归档时间: |
|
| 查看次数: |
17680 次 |
| 最近记录: |