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

Asked

Viewed 467 times

1

I am developing an Android app for pizzeria that generates a list of ingredients from checked Checkbox, IE, the user gives a "check" in the items you want for your pizza as catupiry, pepperoni, onion, etc, and so is assembled the list.

My question is, how do I so that when marking a Checkbox, its value is inserted in the list and, if unchecked, the same record is removed. Follows the corresponding section 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 really the best solution? I have several items that the user can mark and I believe that my code will be very extensive and ineligible this way.

  • I believe this post can help you http://techlovejump.com/android-listview-with-checkbox/

  • Hello Clever. Mto good this post, I redid my lists using this article. Thank you !

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.