I have button that a user selects and I pop up a AlertDialog to OK or CANCEL operation of replacing some data. The OK and CANCEL work fine after I figured out its an ASYNCH process.
And I even stop multiple clicks. However when the user presses OK , I have to save the information out to memory along with a couple of tasks. The OK process takes about three seconds, which is over the edge of impatient people. I want to throw up a indeterminate progress bar with message. However my newness to Java and android is getting in the way.
Exactly what is the ‘context’ in the code below?
public void getData(final View v)
{
if(AlertDialogProcessing==0)
{
final String title="Set Image to Wallpaper";
final String message="Press OK to set as Wallpaper or CANCEL";
final String ok="OK";
final String cancel="CANCEL";
final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
try{
alertbox.setMessage(message);
alertbox.setTitle(title);
alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1)
{
Vibrate(ClickVibrate);
Drawable drawable= getSun(imageSelect);
ProgressDialog dialog = ProgressDialog.show(context, "Loading", "Please wait...", true); //<<<<<<<<<<ERROR at context
AlertDialogProcessing=1;
SaveData(drawable,1);
AlertDialogProcessing=0;
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Data Saved.",Toast.LENGTH_LONG).show();
}
});
alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1){
AlertDialogProcessing=0;
Vibrate(ClickVibrate);
}
});
//alertbox.setCanceledOnTouchOutside(false); // maybe a 4.0 problem
alertbox.show();
} catch(Exception e){
//TODO Handle BadTokenException.
}
}
}
In the
onClick()you are creating a progressDialog and you are performing thesaveData()activity on the UI thread. What you can do is create anAsyncTaskin theOnClick()and in itsonPreExecute()show the dialog box, perform the saving action indoInBackground()and inonPostExecute()dismiss the dialog.The definition of Context is(as per android developer site):
Hope the explanation helps.