1

我的 xml 中有 3 个微调器。

1. one is Product Name
2. one is Product Quantity.
3. and 3rd one is Discount

现在,我为每个调用单独的侦听器..问题是我想使用项目数量来计算最终价格,我以相同的顺序调用了微调器,即我写了名称、数量和折扣..

即使我在 Discount Spinner 上面写了,最后还是调用了 Quantity 的侦听器..我不知道发生了什么..任何人都可以面对这个问题...请帮助我..

这是我的 Spinner 监听器:-

spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener() 
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3)
            {
                strItem=spnSelectItem.getItemAtPosition(arg2).toString();           }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

          ///This method called last..
        spnQty.setOnItemSelectedListener(new OnItemSelectedListener() 
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
            {
                strQty=spnQty.getItemAtPosition(arg2).toString();

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
     ///This method called before spnQty ..
     spnItemDiscount.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
            {
                disc=round(Double.parseDouble(spnItemDiscount.getItemAtPosition(arg2).toString().replace("%", "")));

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
4

1 回答 1

4

为什么不创建一个单独的方法来执行所有计算,如下所示:

private void calculatePrice() {
        String strItem = spnSelectItem.getSelectedItem().toString();
        String qty = spnQty.getSelectedItem().toString();
        String discount = spnItemDiscount.getSelectedItem().toString().replace("%", "");

        //perform calculate and update UI as you like
    }

然后要么为所有三个微调器创建一个侦听器,要么在所有这些微调器中调用它们,例如:

spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            //do some work, update UI or reset qty and discount spinner or whatever
            strItem = spnSelectItem.getItemAtPosition(arg2).toString();

            //call calulcate method
            calculatePrice();
        }
    });
于 2013-02-01T11:41:38.847 回答