dfetter88
dfetter88

Reputation: 5391

Return data from dialog

I am using a dialog as a form for the user to input data. When they click the "OK" button, the dialog is closed, and I need to use the data that was entered. How can I reference this data in the activity once the dialog has closed?

Upvotes: 2

Views: 10429

Answers (2)

idolize
idolize

Reputation: 6653

Get it when the user presses the "OK" button

final EditText input = new EditText(this); // This could also come from an xml resource, in which case you would use findViewById() to access the input

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        mItem.setValue(value); // mItem is a member variable in your Activity
        dialog.dismiss();
    }
});

Upvotes: 5

hpique
hpique

Reputation: 120334

Assign an OnDismissListener to the dialog and pass the data to the activity there.

Alternatively, you can create a dialog activity and return the data as the activity result. See the following link for more info about starting activities and gettings results:

http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Upvotes: 4

Related Questions