1

我有 10 个微调器和 10 个编辑文本(用户可以让多个微调器和编辑文本空白),并且我还有 2 个按钮来对所选微调器对象的值求和并编辑文本。对值求和是成功的,但是当我使用重置按钮回到应用程序首次启动时的初始状态时,我遇到了问题。结果是错误的,即使我已将其设置回默认值,我还是从第一次操作中获得了值。这是我的重置按钮代码

hapus.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            qty1.setText(null);
            qty2.setText(null);
            qty3.setText(null);
            qty4.setText(null);
            qty5.setText(null);
            qty6.setText(null);
            qty7.setText(null);
            qty8.setText(null);
            qty9.setText(null);
            qty10.setText(null);
            spin1.setSelection(0);
            spin2.setSelection(0);
            spin3.setSelection(0);
            spin4.setSelection(0);
            spin5.setSelection(0);
            spin6.setSelection(0);
            spin7.setSelection(0);
            spin8.setSelection(0);
            spin9.setSelection(0);
            spin10.setSelection(0);
            sum.setText(null);
            hasil = 0;
        }
    });

这是我的微调器和求和操作代码的一部分

spin8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value8 = item.toString();
            if (item.equals("null"))
            {
                hrg8 = 0;
            }
            else if (item.equals("Soto Istimewa"))
            {
                hrg8 = 24000;
            }
            else if (item.equals("Soto Biasa"))
            {
                hrg8 = 17000;
            }
            else if (item.equals("Nasi Putih"))
            {
                hrg8 = 4000;
            }
            else if (item.equals("Emping"))
            {
                hrg8 = 2000;
            }
            else if (item.equals("Kerupuk"))
            {
                hrg8 = 1000;
            }
            else if (item.equals("Es Teh"))
            {
                hrg8 = 2000;
            }
            else if (item.equals("Es Teh Manis"))
            {
                hrg8 = 3000;
            }
            else if (item.equals("Es Teh Botol"))
            {
                hrg8 = 5000;
            }
            else if (item.equals("Teh Botol Dingin"))
            {
                hrg8 = 3000;
            }
            else if (item.equals("Air Jeruk Panas"))
            {
                hrg8 = 7000;
            }
            else if (item.equals("Air Jeruk Dingin"))
            {
                hrg8 = 7000;
            }
            else if (item.equals("Aqua"))
            {
                hrg8 = 3000;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });

    spin9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value9 = item.toString();
            if (item.equals("null"))
            {
                hrg9 = 0;
            }
            else if (item.equals("Soto Istimewa"))
            {
                hrg9 = 24000;
            }
            else if (item.equals("Soto Biasa"))
            {
                hrg9 = 17000;
            }
            else if (item.equals("Nasi Putih"))
            {
                hrg9 = 4000;
            }
            else if (item.equals("Emping"))
            {
                hrg9 = 2000;
            }
            else if (item.equals("Kerupuk"))
            {
                hrg9 = 1000;
            }
            else if (item.equals("Es Teh"))
            {
                hrg9 = 2000;
            }
            else if (item.equals("Es Teh Manis"))
            {
                hrg9 = 3000;
            }
            else if (item.equals("Es Teh Botol"))
            {
                hrg9 = 5000;
            }
            else if (item.equals("Teh Botol Dingin"))
            {
                hrg9 = 3000;
            }
            else if (item.equals("Air Jeruk Panas"))
            {
                hrg9 = 7000;
            }
            else if (item.equals("Air Jeruk Dingin"))
            {
                hrg9 = 7000;
            }
            else if (item.equals("Aqua"))
            {
                hrg9 = 3000;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });

    spin10.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value10 = item.toString();
            if (item.equals("null"))
            {
                hrg10 = 0;
            }
            else if (item.equals("Soto Istimewa"))
            {
                hrg10 = 24000;
            }
            else if (item.equals("Soto Biasa"))
            {
                hrg10 = 17000;
            }
            else if (item.equals("Nasi Putih"))
            {
                hrg10 = 4000;
            }
            else if (item.equals("Emping"))
            {
                hrg10 = 2000;
            }
            else if (item.equals("Kerupuk"))
            {
                hrg10 = 1000;
            }
            else if (item.equals("Es Teh"))
            {
                hrg10 = 2000;
            }
            else if (item.equals("Es Teh Manis"))
            {
                hrg10 = 3000;
            }
            else if (item.equals("Es Teh Botol"))
            {
                hrg10 = 5000;
            }
            else if (item.equals("Teh Botol Dingin"))
            {
                hrg10 = 3000;
            }
            else if (item.equals("Air Jeruk Panas"))
            {
                hrg10 = 7000;
            }
            else if (item.equals("Air Jeruk Dingin"))
            {
                hrg10 = 7000;
            }
            else if (item.equals("Aqua"))
            {
                hrg10 = 3000;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });

    hitung.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            try
            {
                if (qty1.getText().toString() != null && !(qty1.getText().toString().equals("")))
                {
                    jml1 = Integer.parseInt(qty1.getText().toString());
                    item1 = hrg1 * jml1;
                }
                else 
                {
                    jml1 = 0;
                }
                if (qty2.getText().toString() != null && !(qty2.getText().toString().equals("")))
                {
                    jml2 = Integer.parseInt(qty2.getText().toString());
                    item2 = hrg2 * jml2;
                }
                else 
                {
                    jml2 = 0;
                }
                if (qty3.getText().toString() != null && !(qty3.getText().toString().equals("")))
                {
                    jml3 = Integer.parseInt(qty3.getText().toString());
                    item3 = hrg3 * jml3;
                }
                else 
                {
                    jml3 = 0;
                }
                if (qty4.getText().toString() != null && !(qty4.getText().toString().equals("")))
                {
                    jml4 = Integer.parseInt(qty4.getText().toString());
                    item4 = hrg4 * jml4;
                }
                else 
                {
                    jml4 = 0;
                }
                if (qty5.getText().toString() != null && !(qty5.getText().toString().equals("")))
                {
                    jml5 = Integer.parseInt(qty5.getText().toString());
                    item5 = hrg5 * jml5;
                }
                else 
                {
                    jml5 = 0;
                }
                if (qty6.getText().toString() != null && !(qty6.getText().toString().equals("")))
                {
                    jml6 = Integer.parseInt(qty6.getText().toString());
                    item6 = hrg6 * jml6;
                }
                else 
                {
                    jml6 = 0;
                }
                if (qty7.getText().toString() != null && !(qty7.getText().toString().equals("")))
                {
                    jml7 = Integer.parseInt(qty7.getText().toString());
                    item7 = hrg7 * jml7;
                }
                else 
                {
                    jml7 = 0;
                }
                if (qty8.getText().toString() != null && !(qty8.getText().toString().equals("")))
                {
                    jml8 = Integer.parseInt(qty8.getText().toString());
                    item8 = hrg8 * jml8;
                }
                else 
                {
                    jml8 = 0;
                }
                if (qty9.getText().toString() != null && !(qty9.getText().toString().equals("")))
                {
                    jml9 = Integer.parseInt(qty9.getText().toString());
                    item9 = hrg9 * jml9;
                }
                else 
                {
                    jml9 = 0;
                }
                if (qty10.getText().toString() != null && !(qty10.getText().toString().equals("")))
                {
                    jml10 = Integer.parseInt(qty10.getText().toString());
                    item10 = hrg10 * jml10;
                }
                else 
                {
                    jml10 = 0;
                }
                hasil = item1 + item2 + item3 + item4 + item5 + item6 + item7 + item8 + item9 + item10;
                Toast.makeText(getApplicationContext(), "Rp. " + hasil, Toast.LENGTH_LONG).show();
                sum.setText("Rp. " + Integer.toString(hasil));
            }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(), "Quantity can't be empty. Please type item quantity", Toast.LENGTH_LONG).show();
            }     
        }
    });

任何帮助将不胜感激,谢谢

4

1 回答 1

2

您正在尝试null用作字符串:

if (item.equals("null"))

它应该是:

if (item == null)

Spinner 或 EditText 的默认值也不是空字符串(即""null。我相信你应该.setText("");在重置值时使用,所以第一个 if 语句应该是:

if (item.isEmpty()) // or item.equals("")

最后,您是否有 10 个 OnItemSelectedListeners 包含 80-90% 的相同代码?如果是这样,您应该编写一个通用侦听器并使用parent.getId()它来确定要设置的值。

于 2012-12-30T04:18:12.887 回答