简体   繁体   中英

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. 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. thank you. (i'm using 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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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