2017-07-26 119 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); 
             } 
            } 
           }); 
          } 
         } 
        } 
       } 
相关问题