更改EditText时设置TextView - 购物车

时间:2015-07-10 12:09:20

标签: android android-edittext textview

在购物车中,我正在Tableview enter image description here

中显示所有对象

当我更改EditText中的数量时,应更改TextView中相应的行总数。我尝试使用addTextChangedListenerTextWatcher以多种方式实现此目的,但它正在更改最后一行总计(TextView)的文本。

TableLayout代码:

for(i=0;i<cartSize;i++)
            {
            pName    = aController.getCart().getProducts(i).getProductName();
            pPrice  = aController.getCart().getProducts(i).getProductPrice();

            final TableRow tr = new TableRow(this);
            tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            product = new TextView(this);
            product.setText(pName);
            product.setId(i);
            product.setPadding(2, 0, 5, 0);
            product.setTextColor(Color.BLACK);
            tr.addView(product);

            price = new TextView(this);
            price.setText(pPrice);
            price.setPadding(2, 0, 5, 0);
            price.setTextColor(Color.BLACK);
            tr.addView(price);

            QTY = new EditText(this);
            QTY.setText("1");
            QTY.setId(i);
            QTY.setInputType(InputType.TYPE_CLASS_NUMBER);
            QTY.setPadding(5, 5, 5, 5);
            QTY.setTextColor(Color.BLACK);
            int maxLength = 6;
            InputFilter[] fArray = new InputFilter[1];
            fArray[0] = new InputFilter.LengthFilter(maxLength);
            QTY.setFilters(fArray);
            QTY.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    int tot = 1;
                    if (s.length() == 0) {
                        total.setText("0");


                    } else {
                        int a = Integer.parseInt(s.toString());
                        tot = Integer.parseInt(pPrice) * a;
                        total.setText("" + tot);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            tr.addView(QTY);

            total = new TextView(this);
            total.setId(i);
            total.setPadding(5, 0, 5, 0);
            total.setTextColor(Color.BLACK);
            total.setText(pPrice);


            tr.addView(total);

            select = new CheckBox(this);
            select.setChecked(true);
            select.setId(i);
            select.setOnClickListener(getOnClickDoSomething(select));
            tr.addView(select);

            tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
        }

现在如何更改总数..

0 个答案:

没有答案