开发者

Call global variable before it is set using AsyncTask

Okay, this has me confused once again. I am trying to either A: set a golbal variable, which i can do or B: retieve a variable from my AsyncTask. I have set can set the golbal variable from asynctask which is fine, but the activity calls it before it is set with the asynctask.

So therefore I need the application to finsh the AsyncTask before calling the golbal variable.

new createUser().execute();                         
Log.i("res", "After: " + Boolean.toString(MyProperties.getInstance().valut));

private class createUser extends AsyncTask<Void, Void, Boolean> {

    ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "",
            "Creating User...", true);
    Toast toast = Toast.makeText(getApplicationContext(), "",
            Toast.LENGTH_SHORT);

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub

        if (db.createUser(nameU.getText().toString(), userU.getText()
                .toString(), emailU.getText().toString(), passU.getText()
               开发者_C百科 .toString()) == false) {
            return false;
        } else {
            return true;
        }
    }

    protected void onPreExecute() {
        dialog.show();
    }

    protected void onPostExecute(Boolean result) {
        dialog.dismiss();
        if (!result) {
            toast.setText("User already exists!");
            toast.show();
            res = result;
            MyProperties.getInstance().valut = res;
            Log.i("res", Boolean.toString(MyProperties.getInstance().valut));

        } else {
            toast.setText("Success");
            toast.show();
            res = result;
            MyProperties.getInstance().valut = res;
            Log.i("res", Boolean.toString(MyProperties.getInstance().valut));
        }
    }
}


Work with your global variable in onPostExecute method of your AsyncTask. You need to implement it in your AsyncTask's child. This method is called then all work is done.

EDIT

private class createUser extends AsyncTask<Void, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub

        if (db.createUser(nameU.getText().toString(), userU.getText()
                .toString(), emailU.getText().toString(), passU.getText()
                .toString()) == false) {
            return false;
        } else {
            return true;
        }
    }

    protected void onPreExecute() {
        dialog = ProgressDialog.show(MainActivity.this, "", "Creating User...", true);
        dialog.show();
    }

    protected void onPostExecute(Boolean result) {
        dialog.dismiss();
        Toast.makeText(
            getApplicationContext(), 
            result?"Success":"User already exists!", 
            Toast.LENGTH_SHORT).show();
        MyProperties.getInstance().valut = result
        Log.i("res", Boolean.toString(MyProperties.getInstance().valut));
    }
}


Two issues:

  • Java does not really have any global variables. The closest it has are static class variables. Since you haven't shown your declared variable, I can't say what you've really implemented.
  • Every AsyncTask has a onPostExecute(..) method you can override that runs in the original thread/Looper after doInBackground(..) has completed. Override this to do things like Update the UI thread.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜