java—每次单击按钮都会向旧布局添加一个动态布局| | android||

a0zr77ik  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(272)

我到处都查过了,但找不到答案。每次点击我都会动态生成订单详情。第一次单击可以正常工作,但之后,每次单击都会将新订单添加到旧订单中。旧的不会消失。我试过了 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在每次结束时清除项目。

wz8daaqr

wz8daaqr1#

对动态视图使用recyclerview,每次单击都会添加新视图,

nimxete2

nimxete22#

尝试使用 removeAllViewsInLayout(); 对于父布局,
例如:

linearLayout.removeAllViewsInLayout();

相关问题