easycheese easycheese - 17 days ago 5
Android Question

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

Answer

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

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