簡體   English   中英

以編程方式使收音機組膨脹時,如何獲取所選的單選按鈕。

[英]How do I get the selected radio button when the radio group is inflated programmatically.

我有一個從數據庫動態構建的布局。 有幾個部分,其中一個是一系列單選組,這些單選組在從數據庫中讀取的每個單選按鈕上均具有自定義文本。

我的代碼正確地放大了布局,添加了正確數量的單選按鈕,並向其中添加了正確的文本。

我不知道的是當它是表行的子級時如何獲取選中的單選按鈕的值。 我沒有使用選中的項目,因為我需要等到用戶確定所有答案都已填寫后才能得到結果。 因此,我有一個帶有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()));
            }

但是在到達第一個日志語句之前它崩潰了。 我已經嘗試過各種變體來獲取子行的子項的值,但是沒有任何效果。

我知道了。 萬一其他人遇到此問題,以備將來參考,您必須讓無線電組成為孩子,然后在其中工作以獲取按鈕。

這是我在主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中的此綿羊記錄進行實際更新。

也許為時已晚,但您可以添加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);

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM