Linearlayout无法删除它的孩子

时间:2017-12-15 05:20:04

标签: java android android-linearlayout

我创建了一个Linearlayout并动态添加了RadioGroup和textView。当我下次调用该方法时,Textview将被删除,但旧的单选按钮不会被删除。

以下是代码:

public void setShippingMethods() {
    shippingChargesLayout.removeAllViews();
    final RadioGroup radioGroup = new RadioGroup(getActivity());

    radioGroup.setOrientation(LinearLayout.VERTICAL);
    TextView textView = new TextView(getActivity());
    textView.setText("Select Shipping Options:");
    shippingChargesLayout.addView(textView);
    for (int i = 0; i < shippingDetailsList.size(); i++) {
        RadioButton radioButton = new RadioButton(getActivity());
        radioButton.setText("" + shippingDetailsList.get(i).getMethodTitle() + "-" + (new SessionManager(getActivity()).getCurrency()) + df.format(shippingDetailsList.get(i).getAmount()));

        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        radioGroup.addView(radioButton, params);
        radioButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }
    shippingChargesLayout.addView(radioGroup);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            shipmentCode = shippingDetailsList.get(i - 1).getCarrierCode();
            Log.v("carrierCode", "" + shipmentCode);
        }
    });

    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (radioGroup.getCheckedRadioButtonId() == -1) {
                Toast.makeText(getActivity(),"Select shipping options.",Toast.LENGTH_LONG).show();
            } else {
                completeOrder();
            }
        }
    });
}

这是图片:

Select Shipping Options

1 个答案:

答案 0 :(得分:0)

我已对您的代码进行了一些更改。

public void setShippingMethods() {
    shippingChargesLayout.removeAllViews();
    final RadioGroup radioGroup = new RadioGroup(getActivity());
    RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    radioGroup.setLayoutParams(params);
    radioGroup.setOrientation(LinearLayout.VERTICAL);
    TextView textView = new TextView(getActivity());
    textView.setText("Select Shipping Options:");
    shippingChargesLayout.addView(textView);
    for (int i = 0; i < shippingDetailsList.size(); i++) {
        RadioButton radioButton = new RadioButton(getActivity());
        radioButton.setText("" + shippingDetailsList.get(i).getMethodTitle() + "-" + (new SessionManager(getActivity()).getCurrency()) + df.format(shippingDetailsList.get(i).getAmount()));
        radioGroup.addView(radioButton);
    }
    shippingChargesLayout.addView(radioGroup);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            shipmentCode = shippingDetailsList.get(i - 1).getCarrierCode();
            Log.v("carrierCode", "" + shipmentCode);
        }
    });

    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (radioGroup.getCheckedRadioButtonId() == -1) {
                Toast.makeText(getActivity(),"Select shipping options.",Toast.LENGTH_LONG).show();
            } else {
                completeOrder();
            }
        }
    });
}