2017-07-26 122 views
0

我在單選按鈕和複選框按鈕上工作。值0爲無線電,1爲複選框。基於這些值,我想在屏幕上顯示它。可能有收音機和複選框。根據數據庫值顯示單選按鈕和複選框

現在,值從數據庫中獲得0和1,它只是設置單選按鈕或複選框的所有屬性。它應該顯示爲值1和單選按鈕複選框值的0。

下面是代碼:

      if(multiSelect != null) 
           { 
            RadioButton radioButton = new RadioButton(mMain); 
            radioButton.setText(name_price); 
            radioButton.setId(i + 6); 
            radioButton.setTextSize(12); 
            radioButton.setTag(attributes.get(num)); 
            radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
            { 
             radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
            } 
            setTextFont(radioButton, "Museo_Slab.otf"); 

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              1f); 
            lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

            radioButton.setLayoutParams(lp); 

            attr_layout[j].addView(radioButton); 
            num++; 

            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            { 
             @Override 
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
             { 
              try 
              { 
               ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

               if (isChecked) 
               { 

                float total_price = current_price + attr_price; 

                item.setItemPrice(total_price + ""); 

                item_price_text.setText(priceStr); 

                selectedAttributes.add(itemAttributes); 
               } 
               // If the attributes are not checked 

              } catch (Exception ex) 
              { 
               GSLogger.e(ex); 
              } 
             } 
            }); 
           } 

           else // if the multiSelect is 1 
           { 
            CheckBox checkBox = new CheckBox(mMain); 
            checkBox.setText(name_price); 
            checkBox.setId(i + 6); 
            checkBox.setTextSize(12); 
            checkBox.setTag(attributes.get(num)); 
            checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
            { 
             checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
            } 
            setTextFont(checkBox, "Museo_Slab.otf"); 

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.WRAP_CONTENT, 
              1f); 
            lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

            checkBox.setLayoutParams(lp); 

            attr_layout[j].addView(checkBox); 
            num++; 

            // Reads the value depending on attribute User Selects 
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            { 
             @Override 
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
             { 
              try 
              { 
               ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

               if (isChecked) 
               { 

                float total_price = current_price + attr_price; 

                item.setItemPrice(total_price + ""); 

                item_price_text.setText(priceStr); 
                selectedAttributes.add(itemAttributes); 
               } 

              } catch (Exception ex) 
              { 
               GSLogger.e(ex); 
              } 
             } 
            }); 
           } 
          } 
         } 
        } 

基於定義的項目組說它是0或1,它應該在屏幕上顯示它。如果組有兩個選項,則屏幕應顯示0值的無線電和1值的複選框。

+0

你正面臨什麼樣的問題? –

回答

1

如果您的條件只允許添加其中之一。你應該檢查值是否爲0,然後添加單選按鈕,如果值是1,它應該添加複選框。你已經實現了if塊,而不是2塊。

if(value == 0) 
    { 
     RadioButton radioButton = new RadioButton(mMain); 
     radioButton.setText(name_price); 
     radioButton.setId(i + 6); 
     radioButton.setTextSize(12); 
     radioButton.setTag(attributes.get(num)); 
     radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
     { 
      radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
     } 
     setTextFont(radioButton, "Museo_Slab.otf"); 

     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       1f); 
     lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

     radioButton.setLayoutParams(lp); 

     attr_layout[j].addView(radioButton); 
     num++; 

     radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       try 
       { 
        ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

        if (isChecked) 
        { 

         float total_price = current_price + attr_price; 

         item.setItemPrice(total_price + ""); 

         item_price_text.setText(priceStr); 

         selectedAttributes.add(itemAttributes); 
        } 
        // If the attributes are not checked 

       } catch (Exception ex) 
       { 
        GSLogger.e(ex); 
       } 
      } 
     }); 
    } 

    if(value==1) 
    { 
     CheckBox checkBox = new CheckBox(mMain); 
     checkBox.setText(name_price); 
     checkBox.setId(i + 6); 
     checkBox.setTextSize(12); 
     checkBox.setTag(attributes.get(num)); 
     checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
     { 
      checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
     } 
     setTextFont(checkBox, "Museo_Slab.otf"); 

     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       1f); 
     lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

     checkBox.setLayoutParams(lp); 

     attr_layout[j].addView(checkBox); 
     num++; 

     // Reads the value depending on attribute User Selects 
     checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       try 
       { 
        ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

        if (isChecked) 
        { 

         float total_price = current_price + attr_price; 

         item.setItemPrice(total_price + ""); 

         item_price_text.setText(priceStr); 
         selectedAttributes.add(itemAttributes); 
        } 

       } catch (Exception ex) 
       { 
        GSLogger.e(ex); 
       } 
      } 
     }); 
    } 
+0

你可以在代碼中進行更改嗎? – Jacky

+0

檢查上面的代碼 – Aditi

+0

仍然一樣。我得到所有項目在複選框.. – Jacky

0

變化,如果狀態,當你從數據庫然後設置單選按鈕,得到0,否則設置複選框

if(value == 0) 
    { 
           RadioButton radioButton = new RadioButton(mMain); 
           radioButton.setText(name_price); 
           radioButton.setId(i + 6); 
           radioButton.setTextSize(12); 
           radioButton.setTag(attributes.get(num)); 
           radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
           { 
            radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           } 
           setTextFont(radioButton, "Museo_Slab.otf"); 

           LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT, 
             LinearLayout.LayoutParams.WRAP_CONTENT, 
             1f); 
           lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

           radioButton.setLayoutParams(lp); 

           attr_layout[j].addView(radioButton); 
           num++; 

           radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
           { 
            @Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            { 
             try 
             { 
              ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

              if (isChecked) 
              { 

               float total_price = current_price + attr_price; 

               item.setItemPrice(total_price + ""); 

               item_price_text.setText(priceStr); 

               selectedAttributes.add(itemAttributes); 
              } 
              // If the attributes are not checked 

             } catch (Exception ex) 
             { 
              GSLogger.e(ex); 
             } 
            } 
           }); 
          } 

          else 
          { 
           CheckBox checkBox = new CheckBox(mMain); 
           checkBox.setText(name_price); 
           checkBox.setId(i + 6); 
           checkBox.setTextSize(12); 
           checkBox.setTag(attributes.get(num)); 
           checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); 
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) 
           { 
            checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           } 
           setTextFont(checkBox, "Museo_Slab.otf"); 

           LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT, 
             LinearLayout.LayoutParams.WRAP_CONTENT, 
             1f); 
           lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom); 

           checkBox.setLayoutParams(lp); 

           attr_layout[j].addView(checkBox); 
           num++; 

           // Reads the value depending on attribute User Selects 
           checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
           { 
            @Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            { 
             try 
             { 
              ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag(); 

              if (isChecked) 
              { 

               float total_price = current_price + attr_price; 

               item.setItemPrice(total_price + ""); 

               item_price_text.setText(priceStr); 
               selectedAttributes.add(itemAttributes); 
              } 

             } catch (Exception ex) 
             { 
              GSLogger.e(ex); 
             } 
            } 
           }); 
          } 
         } 
        } 
       } 
相關問題