-3

Hello I am making an application where I am trying to define the constant using enum and trying to access those constants in the switch case of the doInBackGround method of AsynTask but there it saying me

LOGIN_API_CALL cannot be resolved to a variable

public class TeemWurkAsyncTask extends AsyncTask<String, Void, String> {

    private enum WebAPIConstants {
        LOGIN_API_CALL, FORGOT_PASSWORD_API_CALL;
    }

    private ProgressDialog mProgressDialog;
    private Context mContext;
    private TaskCompleteListener taskCompleteListener;
    private int method;

    public TeemWurkAsyncTask(TaskCompleteListener taskCompleteListener, int method) {
        this.taskCompleteListener = taskCompleteListener;
        this.method = method;
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setTitle(mContext.getString(R.string.app_name));
        mProgressDialog.setMessage(mContext.getString(R.string.please_wait));
        mProgressDialog.setProgressStyle(mProgressDialog.STYLE_SPINNER);
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        switch(method) {
            case LOGIN_API_CALL:  <---- Here getting an error "LOGIN_API_CALL cannot be resolved to a variable"

        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }

}

Please help me and thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

3 Answers3

1

You have several problems: method is an int, not an enum type, and you need to specify WebAPIConstants.LOGIN_API_CALL.

Furthermore, using a switch in this manner is nearly always the Wrong Thing to do; whenever practical, use polymorphism instead of a big switch block. In this specific case, doInBackground really, really doesn't belong to TeemWurkAsyncTask, it belongs to the task being executed, and you should either use Runnable or write your own similar interface that that doInBackground calls to.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

It does not know what is LOGIN_API_CALL because it is enclosed in enum WEBAPIConstants. thats why you need to access it like this

case WebAPIConstants.LOGIN_API_CALL:
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • I tried that also where I am getting "Type mismatch: cannot convert from TeemWurkAsyncTask.WebAPIConstants to int" – N Sharma May 28 '14 at 04:44
  • i guess , at runtime doInBackground runs at seperate thread, thats why it is not able to find that.you need to either create object of asyntask class and then access using that object. – Waqar Ahmed May 28 '14 at 04:46
  • yes it runs at separate thread, I can not create a object of this AsyncTask. I am trying to make a constant file where I am declaring these but when I am trying to access again they are not – N Sharma May 28 '14 at 04:50
  • try access it like this. new TeemWurkAsyncTask ().LOGIN_API_CALL. reference from here .http://stackoverflow.com/questions/663834/in-java-are-enum-types-inside-a-class-static – Waqar Ahmed May 28 '14 at 04:50
  • public class Constants { public static int LOGIN_API_CALL = 1, FORGOT_PASSWORD_API_CALL = 2; // Web API URL } – N Sharma May 28 '14 at 04:52
  • declare variable final also mean public static final int ... and then access it like case Constants.LOGIN_API_CALL : – Waqar Ahmed May 28 '14 at 04:56
  • Oh great, issue was I just simply should have made to LOGIN_API_CALL to final and it worked. Thank you – N Sharma May 28 '14 at 04:59
0

You cannot compare an int with a WebAPIConstants.

Try using the following code:

private WebAPIConstants method;
public TeemWurkAsyncTask(TaskCompleteListener taskCompleteListener, WebAPIConstants method) {
    this.taskCompleteListener = taskCompleteListener;
    this.method = method;
}  

and use :

case WebAPIConstants.LOGIN_API_CALL:
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58