简体   繁体   中英

Remove the Button from the LinearLayout

I am creating a CheckBox list and Button from ArrayList<Integer> , which I am getting as response from the server, programmatically. For updating purpose I am trying to delete the CheckBox list as well as the Button before I add the new one. With the current state the Button is not being deleted I noticed that when the ArrayList becomes empty the Button was not deleted.

How can I delete the Button in the case the table is empty since too there is some cases where the table becomes empty?

    @Override
    public void onAsyncTaskFinished(ArrayList<Integer> result) {

        remove_elements();
        createCheckboxList(result);


    }

private void remove_elements() {
        LinearLayout parent = (LinearLayout) findViewById(R.id.lila);
        for (int i : items) {
            CheckBox ch = (CheckBox) findViewById(i);
            if (ch != null) {
                parent.removeView(ch);
            } else {
                System.out.println("The checkbox is null");
            }    
        }

        Button btn = (Button) findViewById(1);
        if (btn != null) {
            parent.removeView(btn);
        } else {
            System.out.println("The button is null");
        }

    }

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

        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");
        btn.setId(1);
        ll.addView(btn);

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

}

Only create the button if items.size() > 0

if (items.size() > 0){
        Button btn = new Button(this);
        btn.setLayoutParams(new LinearLayout.LayoutParams(500, 150));
        btn.setText("submit");
        btn.setId(1);
        ll.addView(btn);

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

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