简体   繁体   English

如何从复选框中获取多个选中的值并阻止输出显示两次

[英]How to get multiple checked value from checkbox and prevent the output show twice

actually i am trying to create a checkbox that will show match recipe based on multiple selected ingredient. 实际上我正在尝试创建一个复选框,根据多个选定的成分显示匹配配方。 (some recipe may contain same ingredient) But i have no idea how to make the if else statement. (某些食谱可能含有相同的成分)但我不知道如何制作if else陈述。 so far,this is the code to show the selected ingredient, but it is not complete. 到目前为止,这是显示所选成分的代码,但它并不完整。 someone help me with the if else statement please. 有人帮我解决if else声明。 thank you. 谢谢。 (i'm using android studio) (我正在使用android studio)

  public class DessertIngAvail extends Dessert {

ArrayList<String> selection = new ArrayList<String>();
TextView final_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dessert_ing_avail);

    final_text = (TextView)findViewById(R.id.final_result);
    final_text.setEnabled(false);
}

public void selectItem(View view){
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId())
    {
        case R.id.checkBox153:
            if(checked)
            {if(!selection.contains("Bingka Cheese"))
                selection.add("Bingka Cheese");
            }



            break;

        case R.id.checkBox154:
            if(checked)
            {if(!selection.contains("Bingka Cheese"))
                selection.add("Bingka Cheese");

                if(!selection.contains("Steam Shrimp Paste Cake"))
                    selection.add("Steam Shrimp Paste Cake");

                if(!selection.contains("Banana Heart Cake"))
                    selection.add(" Banana Heart Cake");

                if(!selection.contains("Honey Frankincense Cake"))
                    selection.add(" Honey Frankincense Cake");

                if(!selection.contains("Ray Heart Cake"))
                    selection.add("Ray Heart Cake");
            }

            break;

        case R.id.checkBox155:

            if(checked)

            {if(!selection.contains("Bingka Cheese"))
                selection.add("Bingka Cheese");

                if(!selection.contains("Steam Shrimp Paste Cake"))
                    selection.add("Steam Shrimp Paste Cake");

                if(!selection.contains("Banana Heart Cake"))
                    selection.add("Banana Heart Cake");

                if(!selection.contains("Evergreen Cake"))
                    selection.add("Evergreen Cake");

                if(!selection.contains("Moss Cake"))
                    selection.add("Moss Cake");

                if(!selection.contains("Honey Frankincense Cake"))
                    selection.add("Honey Frankincense Cake");

                if(!selection.contains("Ray Heart Cake"))
                    selection.add("Ray Heart Cake");
            }

            break;

        case R.id.checkBox156:
            if(checked)

            {if(!selection.contains("Bingka Cheese"))
                selection.add("Bingka Cheese");}

            break;

        case R.id.checkBox157:

            if(checked)
            {if(!selection.contains("Bingka Cheese"))
                selection.add("Bingka Cheese");
                if(!selection.contains("Steam Shrimp Paste Cake"))
                    selection.add("Steam Shrimp Paste Cake");
                if(!selection.contains("Evergreen Cake"))
                    selection.add("Evergreen Cake");
                if(!selection.contains("Moss Cake"))
                    selection.add("Moss Cake");

            }

            break;



    }
}

public void finalSelection(View view){

    String final_fruit_selection = "";

    for(String Selection : selection)
    {
        final_fruit_selection = final_fruit_selection + Selection + "\n";

    }
    final_text.setText(final_fruit_selection);
    final_text.setEnabled(true);

}

} }

the problem of the code is, i cannot erased the first output of the checked ingredient. 代码的问题是,我无法删除已检查成分的第一个输出。 so, when i enter checked ingredient for the second time, the value of the checked ingredient just keep added without removing the previous result. 因此,当我第二次输入检查成分时,检查成分的值只是保持添加而不删除先前的结果。

First of all use Set (HashSet) to avoid 首先使用Set (HashSet)来避免

if(!selection.contains("Ray Heart Cake")){
    selection.add("Ray Heart Cake");
}

Secondly I would do something like this. 其次我会做这样的事情。 Set ingredient name as View#setTag() or in Map<Integer, String> where Integer is a view id: 将成分名称设置为View#setTag()Map<Integer, String> ,其中Integer是视图ID:

public void selectItem(View view){
    Set<String> output = new HashSet(); // here could be ArrayList also OK
    for(CheckBox ch : allMyCheckBoxes){
        if(ch.isChecked()){
            output.add(ch.getTag());
        }
    }
    showOutPut(output);
}

This would make your code more readable and easy. 这将使您的代码更易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM