2011-01-27 27 views
7

Guysは、onCreateDialogに私はこれを持っている:onPrepareDialogでsetSingleChoiceItemsの内容を設定する方法は?

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

ソリューションは何ですか?ありがとうございます。

答えて

10

AlertDialogクラスのgetListViewメソッドを使用する必要があります。次に、返されたオブジェクトに対してsetItemCheckedメソッドを使用します。例:

2種類の溶液:

alertDialog.getListView().setItemChecked(1, true);

+0

ありがとうございました。 – BoD

+0

すごくありがとうございます。それは本当に働いた.. –

7

が、私はちょうど同じ問題に直面した

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を実行してください。 onPrepareDialog法で


2 /かなりかわいい1

ばかりの新鮮な新しいものでダイアログが使用する古い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); 
    } 
} 

を1つはアンドロイド(froyoソースコードのAlertController.java L. 854)が初めてダイアログを生成するために使用します。

関連する問題