在处理程序中无法执行switch / case语句

时间:2013-08-14 19:30:39

标签: java android switch-statement android-handler

我的Android应用程序遇到了一个问题,我的交换机/案例声明在我的处理程序中无效。我做switch / case语句的主要原因是限制我的处理程序数量。这是我的代码部分:

if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")
                && edittextdollars.length() > 0
                && edittexteuros.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(2);
                        convertvalues("USD", "EUR");
                        img1.setImageDrawable(grabImageFromUrl(imageUrl1));
                    } catch (Exception e) {
                        edittexteuros.setText("Error");
                    }

                }
            });
            thread.start();

        }

        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")
                && edittexteuros.length() > 0
                && edittextdollars.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(3);
                        convertvalues2("EUR", "USD");
                        img1.setImageDrawable(grabImageFromUrl(imageUrl2));
                    } catch (Exception e) {

                    }

                }
            });
            thread.start();

        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD")
                && edittextdollars.length() > 0
                && edittexteuros.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(4);
                        convertvalues("EUR", "USD");
                        img1.setImageDrawable(grabImageFromUrl(imageUrl2));
                    } catch (Exception e) {

                    }

                }
            });
            thread.start();
        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD")
                && edittexteuros.length() > 0
                && edittextdollars.length() == 0) {
            convertvalues2("USD", "EUR");
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(5);
                        convertvalues2("USD", "EUR");
                        img1.setImageDrawable(grabImageFromUrl(imageUrl2));
                    } catch (Exception e) {

                    }

                }
            });
            thread.start();
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR")
                && edittextdollars.length() > 0
                && edittexteuros.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(6);
                        convertEurostoEuros();
                        //img1.setImageDrawable(grabImageFromUrl(imageUrl1));
                    } catch (Exception e) {

                    }

                }
            });
            thread.start();
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR")
                && edittexteuros.length() > 0
                && edittextdollars.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler.sendEmptyMessage(7);
                        OppositeOfConvertEurostoEuros();
                    } catch (Exception e) {

                    }

                }
public Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            dialog1.dismiss();
            try {
            switch (msg.what) {
            case 2:
                img1.setImageDrawable(grabImageFromUrl(imageUrl1));
                convertvalues("USD", "EUR");
            break;
            case 3:
                convertvalues2("EUR", "USD");
                img1.setImageDrawable(grabImageFromUrl(imageUrl2));
            break;
            case 4:
                convertvalues("EUR", "USD");
                img1.setImageDrawable(grabImageFromUrl(imageUrl2));
            break;
            case 5:
                convertvalues2("USD", "EUR");
                img1.setImageDrawable(grabImageFromUrl(imageUrl2));
            break;
            case 6:
                convertEurostoEuros();
            break;
            case 7:
                OppositeOfConvertEurostoEuros();
            break;
            case 8:
                convertDollarstoDollars();
            break;
                        } catch (Exception e) {
                e.printStackTrace();
            }
                         }
                     };

 public String convertvalues(String convertfrom, String convertto) {
        double current;
        double val = Double.parseDouble(edittextdollars.getText()
                .toString());
        DecimalFormat df = new DecimalFormat(".##");
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            current = ycc.convert(convertfrom, convertto);
            edittexteuros.setText(df.format(val * current));
            return "passed";
        } catch (Exception e) {

            return "passed";
        }
    }

    public String convertvalues2(String convertfrom2, String convertto2) {
        double current;
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            current = ycc.convert(convertfrom2, convertto2);
            edittextdollars.setText(df.format(val * current));
            return "passed";
        } catch (Exception e) {

            return "passed";
        }

    }

    protected void convertEurostoEuros() {
        double val = Double.parseDouble(edittextdollars.getText()
                .toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittexteuros.setText(df.format(val*1));
    }

    protected void convertDollarstoDollars() {
        double val = Double.parseDouble(edittextdollars.getText()
                .toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittexteuros.setText(df.format(val*1));
    }

    protected void OppositeOfConvertEurostoEuros() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 1));
    }

    protected void OppositeOfConvertDollarstoDollars() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 1));
    }

我的应用程序所做的是,即使我想找到INR到JPY的汇率,它也会找到USD兑换EUR的汇率。为什么这样做?是因为这是我放入处理程序的第一个案例吗?非常感谢任何有关此问题的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码非常混乱,并且会导致许多问题。您可以通过执行以下操作来大大清理此设计并消除许多这些问题:

  1. text1text2的值直接转换为YahooCurrencyConverter代码。现在你有了代码和代码。具体如何,这取决于text1text2是什么 - 您可以将UI文本的地图存储到转换代码,或使用普通的旧if语句,或使用{ {1}}支持所有这些,或者其他什么。
  2. 如果from和to货币相同,则无需转换。
  3. 如果from和to货币不同,请将转换代码传递到enum并让它完成工作。
  4. 伪代码示例:

    YahooCurrencyConverter

    String code1 = getCurrencyCodeFromUI(text1); String code2 = getCurrencyCodeFromUI(text2); double ratio; if (code1.equals(code2)) { ratio = 1.0; } else { YahooCurrencyConverter ycc = ...; ratio = ycc.convert(code1, code2); } // now ratio stores the conversion ratio, no matter what combination of // currencies was indicated by text1 and text2. 根据UI对象的值获取适当的货币代码。

    基本上,您似乎正在为text1和text2的所有可能组合添加处理程序。相反,如果您首先将text1和text2分别转换为转换代码,则可以让getCurrencyCodeFromUI()处理其余代码。