Android AlertDialog RadioGroup递增?

Android AlertDialog RadioGroup递增?,android,android-identifiers,android-radiogroup,Android,Android Identifiers,Android Radiogroup,我试图在警报对话框中显示一个带有2个按钮的radiogroup I'在单击列表项时动态创建对话框和警报 它工作正常,除了单选按钮ID在每次显示警报时都会增加(如果我点击取消,然后重新点击列表项) 顺便问一下,有没有比创建临时最终变量更好的方法来引用广播组 // List item clicked @Override public void onItemClick(AdapterView<?> oAdapterView, View oView, int iPos, long lArg)

我试图在警报对话框中显示一个带有2个按钮的radiogroup

I'在单击列表项时动态创建对话框和警报

它工作正常,除了单选按钮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();
}
//已单击列表项
@凌驾
公开上市(AdapterView-oAdapterView、View-oView、int-IPO、long-lArg)
{
ListView oListView=(ListView)findViewById(R.id.usage\u room\u list\u lv);
String strClickedItem=(String)oListView.getItemAtPosition(iPos);
AlertDialog.Builder oAlert=新建AlertDialog.Builder(此);
oAlert.setTitle(“警报标题”);
oAlert.setMessage(“警报消息”);
setNegativeButton(getResources().getString(R.string.cancel).toString(),null);
放射组oGroup=新放射组(此);
IRSTButton的RadioButton=新的RadioButton(本);
setText(“第一个按钮”);
RadioButton oSecondButton=新的RadioButton(本);
设置文本(“第二个按钮”);
oGroup.addView(秒按钮);
oGroup.addView(oAccessibleChoice);
//下面的内部设置正按钮需要,有更好的方法吗?
最终放射组oTmpGroup=oGroup;
oAlert.setView(oTmpGroup);
setPositiveButton(“完成”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which)
{
//这会自动递增
if(DEBUG)System.out.println(CLASS_NAME+“单击选项:”+oTmpGroup.getCheckedRadioButtonId());
}
});
oAlert.show();
}

任何大多数视图实现的ID都将自动递增,因为它是在构造函数中指定的。为了克服这个问题,您可以使用
setId(int)
方法重新定义任何视图子类的id


我对“更好”一无所知。您想要什么-更好的可读性、更短的代码或更快的执行时间?您的解决方案已经很好了,可以访问存储在variable fast和don't croot属性列表中的引用。不过,我不明白,如果在其作用域内不更改原始变量的值,为什么要创建新的最终变量。

为了引用此行的组
if(DEBUG)System.out.println(CLASS_NAME+“Clicked option:”+oTmpGroup.getCheckedRadioButtonId())
,则必须将
oTmpGroup
标记为final。您可以删除
oTmpGroup
并将其设置为final
oGroup
,因为您在最初定义它的位置执行
onItemClicked
时不会更改它的值。