I programmed an quiz-app and if I touch one of the answers in an AsyncTask if the answer is correct I set the color to green or if it is false to red.
But now during the time the AsyncTask runs I can press other buttons like the "change question" button or on another one of the answers. This is then done after the AsyncTask has finished it's work. So the next question is loaded and it automatically answers the next question or uses one of the jokers what ever.
I tried to setEnabled(false) the Buttons but they are still bugging.
How do I prevent this?
private void disableOrDisableButtons(boolean boo) {
buttonAnswer1.setEnabled(boo);
buttonAnswer2.setEnabled(boo);
buttonAnswer3.setEnabled(boo);
buttonAnswer4.setEnabled(boo);
}
and here I start the AsyncTask
disableOrDisableButtons(false);
new PrepareAdapter().execute(null, null, null);
in my AsyncTask
@Override
protected void onPreExecute() {
disableOrDisableButtons(false);
if (correctAnswerAtButton != buttonClicked) {
switch (buttonClicked) {
case 1:
buttonAnswer1.setTextColor(Color.RED);
break;
case 2:
buttonAnswer2.setTextColor(Color.RED);
break;
case 3:
buttonAnswer3.setTextColor(Color.RED);
break;
case 4:
buttonAnswer4.setTextColor(Color.RED);
break;
}
if (buttonClicked != 0) { // 0.. if second chance joker used
wrongAnswer = true;
}
}
switch (correctAnswerAtButton) {
case 1:
buttonAnswer1.setTextColor(Color.GREEN);
return;
case 2:
buttonAnswer2.setTextColor(Color.GREEN);
return;
case 3:
buttonAnswer3.setTextColor(Color.GREEN);
return;
case 4:
buttonAnswer4.setTextColor(Color.GREEN);
return;
}
}
I you want to disable the whole interface while the AsyncTask runs, you can use code such as the following to display a dialog:
public abstract class BaseAsyncTask<Param, Result> extends AsyncTask<Param, Void, Result> implements DialogInterface.OnCancelListener {
private static final String TAG = "BaseAsyncTask";
private ProgressDialog dialog = null;
protected Context ctx = null;
protected Exception exception = null;
public BaseAsyncTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(ctx, WLConstants.MSG_TITLE_LOADING_DIALOG, WLConstants.MSG_LOADING_DIALOG, true);
dialog.setCancelable(true);
dialog.setOnCancelListener(this);
if (ctx instanceof WozzonActivity) {
((WozzonActivity) ctx).setCurrentDialog(dialog);
}
}
@Override
protected Result doInBackground(Param... parameters) {
try {
return inBackground(parameters);
} catch (Exception ex) {
exception = ex;
Log.e(TAG, ex.getClass().getName(), ex);
return null;
}
};
@Override
protected void onPostExecute(Result result) {
try {
dialog.dismiss();
} catch (Exception ex) {
}// TODO:
if (result == null) {
onException(exception);
} else {
onResult(result);
}
}
protected void onException(Exception ex) {
if (ex != null && ex instanceof WozzonException) {
Toast.makeText(ctx, ex.getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx, WLConstants._ERROR_MSG, Toast.LENGTH_SHORT).show();
}
}
public abstract void onResult(Result result);
public abstract Result inBackground(Param... parameters) throws Exception;
@Override
public void onCancel(DialogInterface theDialog) {
cancel(true);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With