2011-07-27 20 views
3

これを把握するのに問題があります。私が持っているものは、マルチチェックの警告ダイアログです。私がしたいのは、ポジティブボタンが押されたときに、チェックされたインデックスでタスクが実行されることです。それをどうやって行うことができますか?AlertDialogで選択したすべてのインデックスを取得する方法multiChoiceItems

私は最高だここは...ある

dialog.setMultiChoiceItems(list, null, null); 
dialog.setPositiveButton("Okay", null); 

概要:どのように私はAlertDialogからチェックし、インデックスのすべてを得ることができますか?

答えて

8

final Boolean []は項目の状態を保存すると宣言し、setMultiChoiceItemsメソッドを呼び出すと、DialogInterface.OnMultiChoiceClickListenerが提供されました。変更されたときにこの配列の各項目の状態が設定されました。次に、正のボタンをクリックすると、DialogInterface.OnClickListenerからこの配列を参照できます。例えばので

(コピーされ、少し私のコードの一部から難読化):

final int aIndex = 0; 
    final int bIndex = 1; 
    final int cIndex = 2; 
    final int dIndex = 3; 

    final CharSequence[] items = { 
      context.getString(R.string.string_share_include_a), 
      context.getString(R.string.string_share_include_b), 
      context.getString(R.string.string_share_include_c), 
      context.getString(R.string.string_share_include_d) }; 

    final Boolean[] state = new Boolean[4]; 
    state[aIndex] = true; 
    state[bIndex] = true; 
    state[cIndex] = true; 
    state[dIndex] = false; 

    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle(R.string.string_share_dialog_title); 
    builder.setMultiChoiceItems(items, new boolean[] { true, true, true, 
      false }, new DialogInterface.OnMultiChoiceClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which, 
       boolean isChecked) { 
      state[which] = isChecked; 
     } 
    }); 

    builder.setPositiveButton(R.string.string_share_ok, 
      new OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 

        Utilities.shareStuff(
          state[aIndex], 
          state[bIndex], 
          state[cIndex], 
          state[dIndex]); 
       } 
      }); 
+0

イムが探してまさにです!ありがとう! – thunderousNinja

関連する問題