-1

I have encountered a problem in my Android application where my switch/case statment won't work in my handler. The main reason I did the switch/case statement is to limit the amount of handlers I have. Here is my part of my code:

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));
    }

The thing my app does is that it will find the exchange rate of USD to EUR even if I want to find the exchange rate of INR to JPY. Why does it do this? Is it because it is the first case I put in my handler? Any help regarding this problem is greatly appreciated.

user2507301
  • 153
  • 1
  • 15
  • In the handleMessage you have a try block but you don't catch anything. Why is this and how does this even compile? – Mastergeek Aug 14 '13 at 19:51
  • I do, but I just didn't show it. – user2507301 Aug 14 '13 at 19:56
  • @Mastergeek I have updated my code in response to your comment. – user2507301 Aug 14 '13 at 20:08
  • This code has lots of issues. Why are you manually creating new threads? Why not use something like AsyncTask? You're calling `setImageDrawable` from a non-UI thread (in addition to in the handler). I imagine that's going to give you issues. I don't see INR or JPY anywhere in here. I have no idea what `OppositeOfConvertEurostoEuros` does, but apparently `OppositeOfConvertDollarstoDollars` also does exactly the same thing (it seems to have exactly the same code). I also have no idea why you would need a method to convert Euros to Euros or dollars to dollars. – kabuko Aug 14 '13 at 20:29
  • @kabuko Thank you. I will try to do what you said. – user2507301 Aug 14 '13 at 20:30

1 Answers1

0

Your code is very confusing, and lends itself to a number of problems. You can greatly clean up this design and eliminate a lot of these issues by doing the following:

  1. Convert the value of text1 or text2 directly to a YahooCurrencyConverter code. Now you have a from code and a to code. Exactly how you do this really depends on what text1 and text2 are - you can store a map of UI text to conversion codes, or use a plain old if statement, or use an enum to back all this up, or whatever.
  2. If the from and to currency are the same, no conversion is necessary.
  3. If the from and to currency are different, pass the conversion codes on to YahooCurrencyConverter and let it do the work.

Pseudocode example:

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.

Where getCurrencyCodeFromUI() gets the appropriate currency code based on the value of the UI object.

Basically it looks like you are adding handlers for all possible combinations of text1 and text2. If, instead, you convert text1 and text2, separately, to conversion codes first, you can let YahooCurrencyConverter deal with the rest.

Jason C
  • 38,729
  • 14
  • 126
  • 182