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

时间:2016-05-22 06:31:40

标签: java android if-statement android-studio android-checkbox

实际上我正在尝试创建一个复选框,该复选框将根据多个选定的成分显示匹配配方。 (某些食谱可能含有相同的成分)但我不知道如何制作if else陈述。到目前为止,这是显示所选成分的代码,但它并不完整。有人帮我解释if else声明。谢谢。 (我正在使用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);

}

}

代码的问题是,我无法删除已检查成分的第一个输出。因此,当我第二次输入检查成分时,检查成分的值只是保持添加而不删除先前的结果。

1 个答案:

答案 0 :(得分:0)

首先使用A1, A2 20294 Lorenzana Dr Woodland Hills, CA 91364 B1, B2 19831 Henshaw St Culver City, CA 94023 C1, C2 5142 Dumont Pl Azusa, CA 91112 D1, D2 20636 De Forest St Woodland Hills, CA 91364 A1, A2 20294 Lorenzana Dr Woodland Hills, CA 91364 E1, E2 4851 Poe Ave Woodland Hills, CA 91364 F1, F2 20225 Lorenzana Dr Los Angeles, CA 91111 G1, G2 20253 Lorenzana Dr Los Angeles, CA 90005 H1, H2 5241 Del Moreno Dr Los Angeles, CA 91110 I1, I2 5332 Felice Pl Stevenson Ranch, CA 94135 J1, J2 5135 Quakertown Ave Thousand Oaks, CA 91362 K1, K2 720 Eucalyptus Ave 105 Inglewood, CA 89030 L1, L2 5021 Dumont Pl Woodland Hills, CA 91364 M1, M2 4819 Quedo Pl Westlake Village, CA 91362 I1, I2 5332 Felice Pl Stevenson Ranch, CA 94135 I1, I2 5332 Felice Pl Stevenson Ranch, CA 94135 N1, N2 20044 Wells Dr Beverly Hills, CA 90210 O1, O2 7659 Mckinley Ave Los Angeles, CA 90001 (HashSet)来避免

Set

其次我会做这样的事情。将成分名称设置为if(!selection.contains("Ray Heart Cake")){ selection.add("Ray Heart Cake"); } View#setTag(),其中Map<Integer, String>是视图ID:

Integer

这将使您的代码更易读,更容易。