0

アラート ダイアログに 2 つのボタンがあるラジオグループを表示しようとしています。

リストアイテムのクリックでダイアログとアラートを動的に作成します。

アラートが表示されるたびにラジオボタンIDが増加することを除けば、正常に機能しています(キャンセルを押してからリスト項目を再クリックした場合)。

余談ですが、一時的な最終変数を作成するよりも、ラジオグループを参照する「より良い」方法はありますか?

// List item clicked
@Override
public void onItemClick(AdapterView<?> oAdapterView, View oView, int iPos, long lArg)
{
    ListView oListView = (ListView) findViewById(R.id.usage_room_list_lv);
    String strClickedItem = (String) oListView.getItemAtPosition(iPos);


    AlertDialog.Builder oAlert = new AlertDialog.Builder(this);

    oAlert.setTitle("Alert Title");
    oAlert.setMessage("Alert Message");
    oAlert.setNegativeButton(getResources().getString(R.string.cancel).toString(), null);

    RadioGroup oGroup = new RadioGroup(this);

    RadioButton oFirstButton = new RadioButton(this);
    oFirstButton.setText("First Button");

    RadioButton oSecondButton = new RadioButton(this);
    oSecondButton .setText("Second Button");

    oGroup.addView(oSecondButton);
    oGroup.addView(oAccessibleChoice);

    // Required for inside setPositiveButton below, is there a better way?
    final RadioGroup oTmpGroup = oGroup;
    oAlert.setView(oTmpGroup);

    oAlert.setPositiveButton("Done", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which)
        {
                            // This auto increments
            if (DEBUG) System.out.println(CLASS_NAME + " Clicked option: " + oTmpGroup.getCheckedRadioButtonId());
        }
    });

    oAlert.show();
}
4

1 に答える 1

0

ほとんどの View 実装の ID は、コンストラクターで指定されているため、自動インクリメントされます。この問題を解決するには、setId(int)メソッドを使用して任意の View サブクラスの ID を再定義できます。

「より良い」について何も知らない。読みやすさの向上、コードの短縮、実行時間の短縮など、何を求めていますか? あなたのソリューションはすでに問題なく、変数に保存されている参照にすばやくアクセスし、プロパティリストを乱雑にしないでください。ただし、スコープ中に元の値を変更しない場合、新しい最終変数を作成する理由がわかりません。

于 2014-03-09T09:39:46.570 に答える