【问题标题】:How do I get the selected radio button when the radio group is inflated programmatically.当单选组以编程方式膨胀时,如何获取选定的单选按钮。
【发布时间】:2013-11-25 01:58:48
【问题描述】:

我有一个从数据库动态构建的布局。有几个部分,其中一个是一系列单选组,每个单选按钮上都有自定义文本,从数据库中读取。

我的代码正确地扩展了布局,添加了正确数量的单选按钮并向它们添加了正确的文本。

我不知道的是如何在桌子行的孩子时获取检查收音机按钮的值。我没有使用选中的项目,因为我需要等到用户确定所有答案都已填写后才能获得结果。所以我有一个带有 onClick 的单独按钮,它将读取数据并将其存储在数据库中。

这是我的主要 XML 文件中的区域

这是单独的行 XML

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
  </RadioGroup>

</TableRow>

这是我试图读取所选单选按钮值的地方

table = (TableLayout) findViewById(R.id.TableLayout03);
for( int ii = 0; ii < nRecs3; ii++ ){   
TableRow row1= (TableRow)table.getChildAt(ii);
Log.i("radio button " , " index of row is " + String.valueOf(row1));
View child = table.getChildAt(ii);
int idx = row1.indexOfChild(child);
Log.i("radio button " , " index of child is " + String.valueOf(idx));
RadioGroup radio = (RadioGroup)child;
Log.i("radio button " , " checked button is " + String.valueOf(radio.getCheckedRadioButtonId()));
            }

但它在到达第一个日志语句之前就崩溃了。我已经尝试了各种变体来获取子行的子项的值,但没有任何效果。

【问题讨论】:

  • 发布崩溃日志
  • 我不得不让代码工作以收集数据,因此回滚到不使用膨胀布局并硬编码一个感兴趣的无线电组,然后在不尝试使用子属性的情况下获取所选项目。我回来试图让多个无线电组工作的一般情况。我正在再次推进我的更改。

标签: android layout-inflater radio-group


【解决方案1】:

我让它工作了。为了将来参考以防其他人遇到此问题,您必须让 radiogroup 成为孩子,然后在其中工作以获取按钮。

这是我在主 XML 文件中描述表格的方式

<ScrollView
    android:id="@+id/scroll03"
    android:layout_width="fill_parent"
    android:layout_height="175dp">

        <TableLayout
           android:id="@+id/TableLayout03"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content">
        </TableLayout>  
</ScrollView>   

然后我将其作为行条目的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/radioGroup1_lbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:typeface="monospace" />  

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="175dp">
</RadioGroup>

</TableRow>

这是从数据库创建表行和单选组的代码子集

    // Set up the user traits  
    cmd = String.format("select evaluation_trait_table.trait_name, evaluation_trait_table.id_traitid, " +
            "custom_evaluation_name_table.custom_eval_number " +
            "from evaluation_trait_table inner join last_eval_table on " +
            " evaluation_trait_table.id_traitid = last_eval_table.id_traitid" +
            " inner join custom_evaluation_name_table on evaluation_trait_table.id_traitid = " +
            " custom_evaluation_name_table.id_traitid where evaluation_trait_table.trait_type = 3 ") ;
    crsr = dbh.exec( cmd );
    cursor   = ( Cursor ) crsr;
    startManagingCursor(cursor);
    nRecs3    = cursor.getCount(); // number of user defined traits to use
    dbh.moveToFirstRecord();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        user_evaluation_traits.add(cursor.getString(0));
        user_trait_numbers.add(cursor.getInt(1));
        user_trait_number_items.add(cursor.getInt(2));              
    }
    inflater = getLayoutInflater(); 
    TableLayout table = (TableLayout) findViewById(R.id.TableLayout03); 
    for( int ii = 0; ii < nRecs3; ii++ ){   
        TableRow row = (TableRow)inflater.inflate(R.layout.eval_custom_item, table, false);
        tempLabel = user_evaluation_traits.get(ii);
        //  Set the text for the radiogroup label
        ((TextView)row.findViewById(R.id.radioGroup1_lbl)).setText(tempLabel);
        //  Get the text for the buttons
        tempText = String.valueOf(user_trait_numbers.get(ii));
        cmd = String.format("select custom_evaluation_traits_table.custom_evaluation_item " +
                " from custom_evaluation_traits_table " +
                " where custom_evaluation_traits_table.id_traitid = '%s' "+
                " order by custom_evaluation_traits_table.custom_evaluation_order ASC ", tempText);
        crsr = dbh.exec( cmd );
        cursor   = ( Cursor ) crsr;
        startManagingCursor(cursor);
        nRecs4    = cursor.getCount();
        dbh.moveToFirstRecord();                
        ArrayList buttons = new ArrayList();

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
            buttons.add (cursor.getString(0));
        }
        radioBtnText = (String[]) buttons.toArray(new String [buttons.size()]);
        // Build the radio buttons here
        radioGroup = ((RadioGroup) row.findViewById(R.id.radioGroup1));
        addRadioButtons(user_trait_number_items.get(ii), radioBtnText);         
        table.addView(row);         
    }

这是创建无线电组的方法

private void addRadioButtons(int numButtons, String[] radioBtnText) {
  int i;

  for(i = 0; i < numButtons; i++){
    //instantiate...
    RadioButton radioBtn = new RadioButton(this);

    //set the values that you would otherwise hardcode in the xml...
    radioBtn.setLayoutParams 
      (new LayoutParams 
      (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //label the button...
    radioBtn.setText(radioBtnText[i]);
    Log.i("addradiobuttons", radioBtnText[i]);
    radioBtn.setId(i);

    //add it to the group.
    radioGroup.addView(radioBtn, i);
  }
}        

最后是我实际阅读单选按钮并准备更新数据库的代码子集

        table = (TableLayout) findViewById(R.id.TableLayout03);
        if (nRecs3 != 0) {
            for( int ii = 0; ii < nRecs3; ii++ ){   
                TableRow row1= (TableRow)table.getChildAt(ii);
                tempTrait = user_trait_numbers.get (ii);
                RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
                try {
                    tempRadioBtn = rg.getCheckedRadioButtonId();
                    cmd = String.format("select custom_evaluation_traits_table.id_custom_traitid " +
                            " from custom_evaluation_traits_table " +
                            " where custom_evaluation_traits_table.id_traitid = %s "+
                            " and custom_evaluation_traits_table.custom_evaluation_order =  %s ", tempTrait, tempRadioBtn+1);
                    crsr = dbh.exec( cmd );
                    cursor   = ( Cursor ) crsr;
                    startManagingCursor(cursor);
                    dbh.moveToFirstRecord();                
                    tempRadioBtn = cursor.getInt(0);
                } catch (Exception ex) {
                    tempRadioBtn = 0;
                }
                user_scores.add(tempRadioBtn);
            }
            for( int ii = nRecs3; ii < 5; ii++ ){   
                user_scores.add(0);
            }
        }else {
            for( int ii = 0; ii < 5; ii++ ){
                user_scores.add(0);
            }
        }
//          Fill the user score variables from the user_scores array
        trait16_data = user_scores.get(0);
        trait17_data = user_scores.get(1);
        trait18_data = user_scores.get(2);
        trait19_data = user_scores.get(3);
        trait20_data = user_scores.get(4);

从那里我构建查询来对sheep_evaluation_table中的这条羊记录进行实际更新。

【讨论】:

    【解决方案2】:

    也许为时已晚,但您可以添加 setoncheckedlistener。此日志 ID 和状态。在此之后,您使用 setter 和 getter。 希望这会有所帮助

    private void inflateRadioButton() {
    
            ArrayList arrayList = json_helper_class.getChoice_group_multiple();
    
            for (int i = 0; i < arrayList.size(); i++) {
                LinearLayout row = new LinearLayout(Diagnose_Test_Activity.this);
                row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    
                    final RadioButton radioButton = new RadioButton(Diagnose_Test_Activity.this);
    
                    String a = String.valueOf(arrayList.get(i));
                    radioButton.setText(a);
                    radioButton.setId(i);
    
                    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            String a = String.valueOf(radioButton.getId());
    
                            Log.d(TAG, "onCheckedChanged: "+a+b);
                             //setter here
    
                        }
                    });
    
    
                    row.addView(radioButton);
                ll_group_multiple_container.addView(row);
    
            }
        }
    

    【讨论】:

    • 我试过了,但是因为我不想做任何事情,直到我输入了所有数据并且用户选择了更新数据库按钮,所以我无法让它工作。这不像是在选中单选按钮后立即发生的事情。事实上,这正是我不想要的。虽然适用于其他事情,所以谢谢。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多