Reputation: 77
In my MainActivity
, I generate a DialogAlert
with 2 EditText
s inside.
I want to recover my 2 EditText
s in my MainActivity
when I click on the Positive Button in the DialogAlert
.
Here my DialogAlert
class:
public class ShareDialog extends DialogFragment{
public interface ShareDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
ShareDialogListener mListener;
private Handler mResponseHandler;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (ShareDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ShareDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_share, null);
final EditText name_place = (EditText)v.findViewById(R.id.sharePlaceName);
final EditText description_place = (EditText)v.findViewById(R.id.shareDescription);
builder.setView(v)
.setTitle(R.string.shareTitle)
.setPositiveButton(R.string.share, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String name = name_place.getText().toString();
String description = description_place.getText().toString();
mListener.onDialogPositiveClick(ShareDialog.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(ShareDialog.this);
ShareDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
My MainActivity
implements ShareDialog.ShareDialogListener
In my MainActivity
I have:
public void onClickShare(View v) {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new ShareDialog();
dialog.show(getSupportFragmentManager(), "ShareDialog");
}
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, "Position Shared", Toast.LENGTH_SHORT).show();
}
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel Share", Toast.LENGTH_SHORT).show();
}
When I click the Positive Button in the DialogAlert
, I have a Toast
with "position shared".
I would like to recover in the method onDialogPositiveClick()
from MainActivity
my params name and description created in my DialogAlert
Upvotes: 2
Views: 2146
Reputation: 18670
Just modify the onDialogPositiveClick
method to provide the params:
public interface ShareDialogListener {
public void onDialogPositiveClick(DialogFragment dialog, String name, String description);
public void onDialogNegativeClick(DialogFragment dialog);
}
In your alert:
mListener.onDialogPositiveClick(ShareDialog.this, name_place.getText(), description_place.getText());
And in your activity:
public void onDialogPositiveClick(DialogFragment dialog, String name, String description) {
Toast.makeText(this, "Position Shared: " + name + ", " + description, Toast.LENGTH_SHORT).show();
}
Upvotes: 3