每次单击按钮都会为旧布局添加一个动态布局 ||Android||

Every button click adds a dynamic layout to old layout ||Android||

我到处都查过了,但找不到答案。我在每次点击时动态生成订单详细信息。第一次点击工作正常,但之后,每次点击都会将新订单添加到旧订单中。旧的并没有消失。我尝试了 remove view 但这也无济于事。我该如何解决?我想在点击时显示每个订单详情。


@Override
    public void itemClicked(int index, int siparisId) {
        siparis_id = siparisId;

        getSiparisUrunleri(siparis_id);
    }



private void getSiparisDetay(int siparisId) {

linearUrunler = view.findViewById(R.id.linearUrunler);

        for (SiparisUrunleri siparisUrunleri : siparisUrunleriList) {

            linearTxt = new LinearLayout(context);
            linearTxt.setOrientation(LinearLayout.HORIZONTAL);
            linearTxt.setWeightSum(10f);
            linearTxt.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            TextView txtUrunAdi = new TextView(context);
            txtUrunAdi.setText(siparisUrunleri.getUrun_adi());
            txtUrunAdi.setId(siparisUrunleri.getId());
            txtUrunAdi.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 8f));
            txtUrunAdi.setPadding(3, 3, 3, 3);

            TextView txtUrunAdet = new TextView(context);
            txtUrunAdet.setId(siparisUrunleri.getId());
            txtUrunAdet.setText(MessageFormat.format("{0} Adet", siparisUrunleri.getAdet()));
            txtUrunAdet.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            txtUrunAdet.setPadding(3, 3, 3, 3);

            TextView txtUrunOlcu = new TextView(context);
            txtUrunOlcu.setId(siparisUrunleri.getId());
            txtUrunOlcu.setText(MessageFormat.format("{0}{1}", parseInputByReplace(siparisUrunleri.getOlcu()), getResources().getString(R.string.meter_square_sign)));
            txtUrunOlcu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            txtUrunOlcu.setPadding(3, 3, 3, 3);

            linearTxt.addView(txtUrunAdi);
            linearTxt.addView(txtUrunAdet);
            linearTxt.addView(txtUrunOlcu);

            linearLayout.addView(linearTxt);
        }

        linearUrunler.addView(linearLayout);

 crdBtnX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ((ViewGroup) linearUrunler.getParent()).removeView(linearLayout);

                relativeSiparisDetay.setVisibility(View.GONE);
            }
        });
}

siparisUrunleriList 在每次关闭时清除项目。

父布局尽量使用removeAllViewsInLayout();

例如:

linearLayout.removeAllViewsInLayout();