显示 AlertDialog 的方法似乎乱序执行
Method that shows AlertDialog seems to be executing out of order
我有一个名为 getCustomAddress()
的方法,它应该显示 AlertDialog
,并保存用户输入的地址。这个方法好像是在方法getTransportType()
和getGoogleDirections
之后执行的。我希望 getCustomAddress
在这些其他方法之前执行并显示 AlertDialog
。
我确实使用调试器逐步完成了这些方法。根据调试器,#2 处的 if 语句实际上评估为真(当我从 spinner/drop 向下选择 "Custom" 时)。
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")) {
getCustomAddress();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});
这是整个 MainActivity.java 文件:
public class MainActivity extends Activity {
private String friendlyLocation = "";
private String address = "";
private String mode = "@mode=d"; //mode b is bicycling, mode d is for driving, mode t is for transit, mode w is for walking
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner addressFieldSpinner = (Spinner) findViewById(R.id.dropdownEditText);
final Button launchMapButton = (Button) findViewById(R.id.launchMapButton);
final RadioButton googTransitButton = (RadioButton) findViewById(R.id.googTransitButton);
final RadioButton googBikeButton = (RadioButton) findViewById(R.id.googBicycleButton);
final RadioButton googDrivingButton = (RadioButton) findViewById(R.id.googDrivingButton);
final RadioButton googWalkingButton = (RadioButton) findViewById(R.id.googWalkButton);
final Button exitButton = (Button)findViewById(R.id.exit_button);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.locations_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
addressFieldSpinner.setAdapter(adapter);
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")){
getCustomAddress();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});
View.OnClickListener exit = new View.OnClickListener(){
@Override
public void onClick(View view) {
finish();
}
};
exitButton.setOnClickListener(exit);
}
private void getDestination(Spinner addressFieldSpinner) {
friendlyLocation = addressFieldSpinner.getSelectedItem().toString();
if (friendlyLocation.equals("Home")) {
address = getString(R.string.homeAddress);
} else if (friendlyLocation.equals("Sarahs house")) {
address = getString(R.string.SarahsAddress);
} else if (friendlyLocation.equals("Work")) {
address = getString(R.string.workAddress);
} else if (friendlyLocation.equals("Custom")) {
address = "Custom";
} else {
//do nothing
}
}
private String GetTransportType(RadioButton googTransitButton, RadioButton googBikeButton, RadioButton googDrivingButton, RadioButton
googWalkingButton) {
if (googBikeButton.isChecked()){
mode = "&mode=b"; //user chose biking
}
else if (googDrivingButton.isChecked()){
mode = "&mode=d"; //user choose driving
}
else if (googTransitButton.isChecked()) {
mode = "&mode=transit"; //user chose public transit
}
else if (googWalkingButton.isChecked()){
mode = "&mode=w"; //user chose walking
}
return mode;
}
private void getGoogleDirections(String mode) {
boolean connected = isConnected();
if (!connected){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Mappy is sorry...")
.setIcon(R.mipmap.ic_launcher)
.setMessage("You do not have a mobile or wifi connection with internet service at this time.")
.setCancelable(false)
.setNegativeButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
else if (address == null || address == "") {
Toast.makeText(MainActivity.this, "It looks like you select custom address, but did not enter an address.", Toast.LENGTH_LONG).show();
}
else {
try {
address = address.replace(' ', '+');
Uri mapsIntentUri = Uri.parse("google.navigation:q=" + address + mode);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapsIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
catch (Exception exception) {
}
}
}
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
friendlyLocation = parent.getItemAtPosition(pos).toString();
/* if (friendlyLocation.equals("Home")) {
address = "2617 SE 35th Place";
} else if (friendlyLocation.equals("Sarahs house")) {
address = "3946 NE Failing, Portland, OR 97212";
} else if (friendlyLocation.equals("Work")) {
address = "619 SW 11th Avenue, Portland, OR 97205";
} else if (friendlyLocation.equals("Custom")) {
address = getCustomAddress();
} else {
}*/
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
private String getCustomAddress() {
final EditText addressEditText = new EditText(this);
final String[] customAddress = {""};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
}
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create().show();
return customAddress[0];
}
//Checks for mobile or wifi connectivity, returns true for connected, false otherwise
private boolean isConnected() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED;
}
}
是的。它是乱序的,因为无论 AlertDialog
是否仍在显示,方法 getCustomAddress
都已完成。当您看到该对话框时,这意味着 alertDialog.create().show();
已完成,然后 return 从 getCustomAddress
执行下一行 GetTransportType
。一个简单的解决方法是:
private String getCustomAddress() {
final EditText addressEditText = new EditText(this);
final String[] customAddress = {""};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
//add call to ggtransporttype here********
doNext();//or you can use a handler here!!
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create().show();
return customAddress[0];
}
private void doNext(){
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
解决此问题的一种方法是在调用任何方法之前先创建对话框,然后在需要时显示。
先声明一个成员在MainActivity
:
private AlertDialog.Builder alertDialog;
然后在您的 MainActivity's onCreate()
中您可以创建(不显示)对话框:
final String[] customAddress = {""};
final EditText addressEditText = new EditText(this);
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
}
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create();
您甚至可以提取用于以不同的代码模块化方法创建对话框的代码。
现在您可以在调用其他方法之前显示该对话框:
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")){
alertDialog.show();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});
我有一个名为 getCustomAddress()
的方法,它应该显示 AlertDialog
,并保存用户输入的地址。这个方法好像是在方法getTransportType()
和getGoogleDirections
之后执行的。我希望 getCustomAddress
在这些其他方法之前执行并显示 AlertDialog
。
我确实使用调试器逐步完成了这些方法。根据调试器,#2 处的 if 语句实际上评估为真(当我从 spinner/drop 向下选择 "Custom" 时)。
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")) {
getCustomAddress();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});
这是整个 MainActivity.java 文件:
public class MainActivity extends Activity {
private String friendlyLocation = "";
private String address = "";
private String mode = "@mode=d"; //mode b is bicycling, mode d is for driving, mode t is for transit, mode w is for walking
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner addressFieldSpinner = (Spinner) findViewById(R.id.dropdownEditText);
final Button launchMapButton = (Button) findViewById(R.id.launchMapButton);
final RadioButton googTransitButton = (RadioButton) findViewById(R.id.googTransitButton);
final RadioButton googBikeButton = (RadioButton) findViewById(R.id.googBicycleButton);
final RadioButton googDrivingButton = (RadioButton) findViewById(R.id.googDrivingButton);
final RadioButton googWalkingButton = (RadioButton) findViewById(R.id.googWalkButton);
final Button exitButton = (Button)findViewById(R.id.exit_button);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.locations_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
addressFieldSpinner.setAdapter(adapter);
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")){
getCustomAddress();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});
View.OnClickListener exit = new View.OnClickListener(){
@Override
public void onClick(View view) {
finish();
}
};
exitButton.setOnClickListener(exit);
}
private void getDestination(Spinner addressFieldSpinner) {
friendlyLocation = addressFieldSpinner.getSelectedItem().toString();
if (friendlyLocation.equals("Home")) {
address = getString(R.string.homeAddress);
} else if (friendlyLocation.equals("Sarahs house")) {
address = getString(R.string.SarahsAddress);
} else if (friendlyLocation.equals("Work")) {
address = getString(R.string.workAddress);
} else if (friendlyLocation.equals("Custom")) {
address = "Custom";
} else {
//do nothing
}
}
private String GetTransportType(RadioButton googTransitButton, RadioButton googBikeButton, RadioButton googDrivingButton, RadioButton
googWalkingButton) {
if (googBikeButton.isChecked()){
mode = "&mode=b"; //user chose biking
}
else if (googDrivingButton.isChecked()){
mode = "&mode=d"; //user choose driving
}
else if (googTransitButton.isChecked()) {
mode = "&mode=transit"; //user chose public transit
}
else if (googWalkingButton.isChecked()){
mode = "&mode=w"; //user chose walking
}
return mode;
}
private void getGoogleDirections(String mode) {
boolean connected = isConnected();
if (!connected){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Mappy is sorry...")
.setIcon(R.mipmap.ic_launcher)
.setMessage("You do not have a mobile or wifi connection with internet service at this time.")
.setCancelable(false)
.setNegativeButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
else if (address == null || address == "") {
Toast.makeText(MainActivity.this, "It looks like you select custom address, but did not enter an address.", Toast.LENGTH_LONG).show();
}
else {
try {
address = address.replace(' ', '+');
Uri mapsIntentUri = Uri.parse("google.navigation:q=" + address + mode);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapsIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
catch (Exception exception) {
}
}
}
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
friendlyLocation = parent.getItemAtPosition(pos).toString();
/* if (friendlyLocation.equals("Home")) {
address = "2617 SE 35th Place";
} else if (friendlyLocation.equals("Sarahs house")) {
address = "3946 NE Failing, Portland, OR 97212";
} else if (friendlyLocation.equals("Work")) {
address = "619 SW 11th Avenue, Portland, OR 97205";
} else if (friendlyLocation.equals("Custom")) {
address = getCustomAddress();
} else {
}*/
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
private String getCustomAddress() {
final EditText addressEditText = new EditText(this);
final String[] customAddress = {""};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
}
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create().show();
return customAddress[0];
}
//Checks for mobile or wifi connectivity, returns true for connected, false otherwise
private boolean isConnected() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED;
}
}
是的。它是乱序的,因为无论 AlertDialog
是否仍在显示,方法 getCustomAddress
都已完成。当您看到该对话框时,这意味着 alertDialog.create().show();
已完成,然后 return 从 getCustomAddress
执行下一行 GetTransportType
。一个简单的解决方法是:
private String getCustomAddress() {
final EditText addressEditText = new EditText(this);
final String[] customAddress = {""};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
//add call to ggtransporttype here********
doNext();//or you can use a handler here!!
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create().show();
return customAddress[0];
}
private void doNext(){
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
解决此问题的一种方法是在调用任何方法之前先创建对话框,然后在需要时显示。
先声明一个成员在MainActivity
:
private AlertDialog.Builder alertDialog;
然后在您的 MainActivity's onCreate()
中您可以创建(不显示)对话框:
final String[] customAddress = {""};
final EditText addressEditText = new EditText(this);
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Please enter destination address");
alertDialog.setView(addressEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton){
customAddress[0] = addressEditText.getText().toString();
}
});
alertDialog.setNegativeButton("CANCEL", null);
alertDialog.create();
您甚至可以提取用于以不同的代码模块化方法创建对话框的代码。 现在您可以在调用其他方法之前显示该对话框:
launchMapButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
//1. Find out where user wants to go
getDestination(addressFieldSpinner);
//2. Check for custom address
if (address.equals("Custom")){
alertDialog.show();
}
//3. Find out what transportation method user wants
GetTransportType(googTransitButton, googBikeButton, googDrivingButton, googWalkingButton);
//4. Get directions from Google Maps
getGoogleDirections(mode);
}
});