简体   繁体   中英

Delete checkbox and button from the LinearLayout

I am creating a checkbox list with a button programmatically. For updating purpose I need to delete the old checkbox list and button before creating the new one in the delete_element method. How can I delete the checkbox and the button from the LinearLayout?

I appreciate any help.

    private void createCheckboxList(final ArrayList<Integer> items) {
        final ArrayList<Integer> selected = new ArrayList<Integer>();

        final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
        for (int i = 0; i < items.size(); i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText(String.valueOf(items.get(i)));
            cb.setId(items.get(i));
            ll.addView(cb);

        }
        Button btn = new Button(this);
        btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
        btn.setText("submit");
        ll.addView(btn);

        btn.setOnClickListener(new View.OnClickListener() {}

}

I think what you need do is to rethink your design, you need to go by Adapter approach. A listView is created with your child elements populated by an adapter which holds the values of child elements. The selected or particular position child item can added or deleted or modified or saved.

Have a look at this link for Adapter Listview example: http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html

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