java - 根据数据库值显示单选按钮和复选框

标签 java android

我正在研究单选按钮和复选框按钮。值 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 值的复选框。

最佳答案

您的 if 条件仅允许添加其中一个。您应该检查该值是否为 0,然后添加单选按钮,如果值为 1,则应添加复选框。您已经实现了 if else block 而不是 2 个 if block 。

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

关于java - 根据数据库值显示单选按钮和复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322403/

相关文章:

java - Libgdx |当 Actor 添加 2 个相同的 Action 时会发生什么

Android Intent 启动应用程序的主要 Activity

java - 当工厂需要创建具有多个公共(public)参数的对象时,如何解决困境?

java - 如何缓冲字节几秒钟然后发送它们

java - Base64编码-解码(Java中)支持数学符号吗?

java - 将数据从一个 Activity 传递到另一个 Activity ,然后打印

java - 从列表返回二维数组未正确索引

android - 错误 : Unable to resolve dependency for ':react-native-maps@debug/compileClasspath' : Could not resolve androidx. appcompat :appcompat:1. 0.0

android - 如何自定义 float ActionBar的背景?

android - Jetpack compose dialogFragment 等效