I have created a dialog which contains a list view. The onclicklistener for the list view dialog then loads a new, inner dialog asking the user to confirm their choice.
Once this is done i want to close both the outer and inner dialog boxes. I am able to close the inner one no problem but am struggling to figure out a way to close the outer dialog which contains a list view from the inner.
An extract of my code is below:
// Display list of sites
AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setTitle("Sites Near Me");
ListView modeList = new ListView(this);
ArrayList<String> stringArrayList = new ArrayList<String>();
for (int i = 0; i < possibleLocaitons.size(); i++) {
String currentLocation = possibleLocaitons.get(i)
.getName();
stringArrayList.add(currentLocation);
}
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,
android.R.id.text1, stringArrayList);
modeList.setAdapter(modeAdapter);
// List click listener
modeList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int pos, long id) {
String site = parent.getItemAtPosition(pos)
.toString();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
LocationActivity.this);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("Check in at " + site + "?")
.setCancelable(false)
.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
// Update the database
UserFunctions us = new UserFunctions();
us.updateLocation(
"fish", "888");
dialog.cancel();
}
})
.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder
.create();
// show it
alertDialog.show();
}
});
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();
}
Any help greatly appreciated.
Edit:
Here is a screenshot of what I am trying to achieve:
http://dl.dropbox.com/u/57441159/Screenshot_2012-10-21-10-13-21.png
When “yes” is clicked i want to cancel both the “sites near me” and “check in at” dialogs, returning to the map activity in the background. Currently it only cancels “check in at” then returns to “sites near me”. I hope this is more clear.
You cannot access this piece of code from alertDialogBuilder’s anonymous onClick() since it is forward referenced
Move it under
builder.setTitle("Sites Near Me");. The proceed to dismiss the dialog from a UI thread using activity.runOnUiThread().