easycheese
easycheese

Reputation: 5899

If Else Android (Else always activating)

I have the following code in an AsyncTask. name is either "item", "setMax" or a sentence like "Creating Database (this is a one-time function)."

My problem comes when I pass "item" into the code the code increments by 1 but then it sets the message to be "item Please wait...". I think it is because it should be If/Else If/Else in sequence but I am not sure. Is there a more efficient way or should I define constants and use a switch statement?

protected void onProgressUpdate(String... name) {
    if (name[0].equals("item")) {
        mDialog.incrementProgressBy(1);
    } if (name[0].equals("setMax")) {
        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mDialog.setMax(Integer.parseInt(name[1]));
    } else {
        mDialog.setMessage(name[0] + " Please wait...");
    }
}

Upvotes: 4

Views: 102746

Answers (2)

CamelSlack
CamelSlack

Reputation: 573

protected void onProgressUpdate(String... name) {
    if (name[0].equals("item")) {
        mDialog.incrementProgressBy(1);
    }else if (name[0].equals("setMax")) {
        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mDialog.setMax(Integer.parseInt(name[1]));
    } else {
        mDialog.setMessage(name[0] + " Please wait...");
    }
}

You were missing an else

Upvotes: 5

K-ballo
K-ballo

Reputation: 81409

It looks from your indentation that you are missing an else:

if (name[0].equals("item")) {
    ...
} else if (name[0].equals("setMax")) {
    ...
} else {
    ...
}

Upvotes: 22

Related Questions