Friday, July 30, 2010

[android-developers] AlertDialog changes sequence of events

Hello,

The following code runs as I expect it to.: (cntr is an int with the
whole class as it's scope)

System.out.println("before saveReading, cntr = " +
Integer.toString(cntr));
saveReading(Integer.parseInt(etRead.getText().toString()));
System.out.println("after saveReading, cntr = " +
Integer.toString(cntr));

public void saveReading(int reading){
System.out.println("In saveReading before ++, cntr = " +
Integer.toString(cntr));
++cntr;
System.out.println("In saveReading, after ++, cntr = " +
Integer.toString(cntr));
}

after the code runs, LogCat shows the following System.out:

System.out before saveReading, cntr = 0
System.out In saveReading before ++, cntr = 0
System.out In saveReading, after ++, cntr = 1
System.out after saveReading, cntr = 1

This (above) is the sequence of System.out events that I am expecting.

//
************************************************************************
If I change the code to include a AlertDialog Yes/No for confirmation
of the ++cntr, the code looks like this:

System.out.println("before saveReading, cntr = " +
Integer.toString(cntr));
saveReading(Integer.parseInt(etRead.getText().toString()));
System.out.println("after saveReading, cntr = " +
Integer.toString(cntr));

public void saveReading(int reading){
System.out.println("In saveReading before ++, cntr = " +
Integer.toString(cntr));
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("Confirmation Required") ;
bldr.setMessage("Save this reading?");
bldr.setPositiveButton(R.string.YesButton, new
DialogInterface.OnClickListener(){
//@Override
public void onClick(DialogInterface dialog, int which) {
///code for YES (increment cntr)
++cntr;
}
});

bldr.setNegativeButton(R.string.NoButton , new
DialogInterface.OnClickListener(){
//@Override
public void onClick(DialogInterface dialog, int which) {
// code for NO (do nothing, cntr remains the same)
System.out.println("no!!!");
}
});

bldr.show();
System.out.println("In saveReading, after ++, cntr = " +
Integer.toString(cntr));
}

When I run the code, the AlertDialog Yes/No routine looks like I want
it to look but the following LogCat output shows that the sequence of
events is not what I was expecting. It looks like all the code in the
code block containing the call for saveReading, including saveReading,
runs and then the code in saveReading runs.

after the code runs, LogCat shows the following System.out:

System.out before saveReading, cntr = 0
System.out after saveReading, cntr = 0
System.out In saveReading before ++, cntr = 0
System.out In saveReading, after ++, cntr = 1

What can I do to make the code with the yes/no confirmation run in the
sequence I am expecting, i.e. like the first LogCat output?

Thank you very much for your help. John Brown

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment