2013-11-25 24 views
0

我有一個從數據庫動態構建的佈局。有幾個部分,其中一部分是從數據庫中讀取的每個單選按鈕上的一系列無線電組。當收音機組以編程方式膨脹時,如何獲得選定的單選按鈕。

我的代碼是正確地膨脹佈局,添加正確數量的單選按鈕並向他們添加正確的文本。

什麼我不明白是如何獲取選中的單選按鈕,當它是一個錶行的孩子的值。我沒有使用選中的項目,因爲我需要等到用戶確定所有答案都已填寫後才能得到結果。所以我有一個單獨的按鈕,它會讀取數據並將其存儲在數據庫中。

這裏是我的主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())); 
      } 

但它崩潰了在它到達第一個日誌語句之前。我已經嘗試了各種各樣的變化,以獲得孩子行的孩子的價值,但沒有任何工作。

+0

後從崩潰 – codeMagic

+0

的logcat的我不得不代碼的工作,收集數據,以便回滾到未使用膨脹佈局和硬編碼感興趣的無線電設備組,然後得到而不嘗試使用該子項目。我回來試圖弄清楚多個無線電小組的一般情況。我正在將我的更改再次推進。 – OogieM

回答

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);   
    } 

這裏是crea TES的RadioGroup中

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中。

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); 

     } 
    } 
+0

我嘗試過,但因爲我不想做任何事情,直到我輸入所有數據,並且用戶選擇更新數據庫按鈕,我無法使其工作。一旦單選按鈕被選中,就不會發生任何事情。事實上,這正是我不想要的。儘管如此,謝謝。 – OogieM

相關問題