簡體   English   中英

從LinearLayout中刪除按鈕

[英]Remove the Button from the LinearLayout

我正在從ArrayList<Integer>創建一個CheckBox列表和Button ,以編程方式從服務器得到響應。 為了進行更新,我嘗試在添加新CheckBox之前刪除CheckBox列表以及Button 在當前狀態下, Button未被刪除,我注意到當ArrayList變為空時, Button未被刪除。

在某些情況下該表為空的情況下,如何刪除表中的Button

    @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() {}

}

僅在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() {}
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM