如何根据给定输入的变化更新整个输出行?

How to update whole output row on change of given Input?

我的输出屏幕有问题!

当我点击带有这些输入的 Click 按钮时,输出屏幕看起来像附加的图像,这太棒了!

但是如果我更改我的输入,程序会通过添加更多包含以前答案的行来给出新答案!我只想在屏幕上显示新答案! 如果我点击按钮同样的方式屏幕添加新行也没有更新输入! 我也附上了这张照片..

我使用了下面给出的代码,

clickbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aI = Double.parseDouble(Ia.getText().toString());
                bI = Double.parseDouble(Ib.getText().toString());
                cI = Double.parseDouble(Ic.getText().toString());
                bisection();
            }
        });

private void bisection() {

        if ((f(aI) < 0 && f(bI) > 0) || (f(aI) > 0 && f(bI) < 0)) {
            for (int i = 0; i < cI; i++) {

                View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item, null, false);
                TextView iteration = (TextView) tableRow.findViewById(R.id.index);
                TextView a = (TextView) tableRow.findViewById(R.id.a);
                TextView b = (TextView) tableRow.findViewById(R.id.b);
                TextView x = (TextView) tableRow.findViewById(R.id.x);
                TextView fx = (TextView) tableRow.findViewById(R.id.fx);

                double root = (aI+bI)/2;

                iteration.setText(" " + (i + 1));
                a.setText(Double.toString(aI));
                b.setText(Double.toString(bI));
                x.setText(Double.toString(root));
                fx.setText(Double.toString(f(root)));
                tableLayout.addView(tableRow);

                if(f(aI)*f(root) < 0){
                    bI = root;
                }else if (f(aI)*f(root) >0) {
                    aI = root;
                }
            }
        } else {
            Toast.makeText(getApplicationContext(), R.string.popUpMsg,Toast.LENGTH_SHORT).show();
        }
    }

    public static double f(double x){
        return ((Math.pow(x,3))-x-4);

    }

我已经发现几乎相同的问题已经在 post 之前被别人问过,但我无法解决我的问题!帮我。谢谢!

第一种方式;你必须使用

removeAllViews();

添加之前。为此,您必须有一个布局变量。例如;

linearLayout.removeAllViews();

另一种方法是在 VIEW.INVISIBLE 或 VIEW.GONE

开头添加 Textview

哪个适合你。

在您的按钮点击侦听器方法中,在调用 bisection() 之前添加以下代码行。

tableLayout.removeAllViews();

只要您点击按钮,就会在计算新的输入值之前删除之前的输出。

您的代码如下所示:

clickbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            aI = Double.parseDouble(Ia.getText().toString());
            bI = Double.parseDouble(Ib.getText().toString());
            cI = Double.parseDouble(Ic.getText().toString());
            tableLayout.removeAllViews();  // Remove previous output
            bisection();
        }
    });