gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:30:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com