How to generate a list with data from an Android Checkbox?

1

I'm developing an Android app for pizzeria that generates a list of checked CheckBox ingredients, that is, the user gives a "check" on the items they want for their pizza like catupiry, calabresa, onion, etc., and so on. the list is mounted.

My question is, how do I make a CheckBox tick if its value is inserted in the list and, if it does not, the same record is removed. Here is the corresponding snippet of this code:

    CheckBox cbOp1 = (CheckBox) findViewById(R.id.cbop1);
    CheckBox cbOp2 = (CheckBox) findViewById(R.id.cbop2);

    final ListView lvOp12 = (ListView) findViewById(R.id.lvop12);

    final List<String> alPedido = new ArrayList<String>();

    if (cbOp1.isChecked()) {
        alPedido.add(cbOp1.getText().toString());
    } else if (alPedido.contains(cbOp1.getText().toString())) {
        alPedido.remove(cbOp1.getText().toString());
    }

    if (cbOp2.isChecked()) {
        alPedido.add(cbOp2.getText().toString());
    } else if (alPedido.contains(cbOp2.getText().toString())) {
        alPedido.remove(cbOp2.getText().toString());
    }

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alPedido);

    lvOp12.setAdapter(arrayAdapter);

I wonder if this is indeed the best solution? I have several items that the user can mark and I believe my code will be too long and ineligible in this way.

    
asked by anonymous 19.10.2016 / 02:11

0 answers