当编程方式对广播组进行充气时,如何获取所选的单选按钮。

时间:2013-11-25 01:58:49

标签: android layout-inflater radio-group

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

我的代码正确地夸大了布局,添加了正确数量的单选按钮并向其添加了正确的文本。

我无法弄清楚当它是表格行的子节点时如何获取已选中单选按钮的值。我没有使用选择的项目,因为我需要等到用户确定在获得结果之前填写了所有答案。所以我有一个带有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()));
            }

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

2 个答案:

答案 0 :(得分:0)

我得到了它的工作。如果其他人有这个问题,为了将来参考,您必须让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中对这条绵羊记录进行实际更新。

答案 1 :(得分:0)

也许为时已晚,但你可以添加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);

        }
    }