Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...");
    }
}
like image 918
easycheese Avatar asked Nov 28 '22 18:11

easycheese


2 Answers

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

if (name[0].equals("item")) {
    ...
} else if (name[0].equals("setMax")) {
    ...
} else {
    ...
}
like image 81
K-ballo Avatar answered Nov 30 '22 06:11

K-ballo


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

like image 25
CamelSlack Avatar answered Nov 30 '22 06:11

CamelSlack