I'm trying to use an interface to return data from a DialogFragment
to the ArrayAdapter
from which it's shown.
I've read something similar here, but I don't know how to call in the DialogFragment
the function returning the data.
Anybody can help?
MyDialog.java
public class MyDialog extends DialogFragment {
static interface Listener {
void returnData(int result);
}
/* ... */
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mBtnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// How can I call PCListAdapter.returnData ?
dismiss();
}
});
}
}
PCListAdapter.java
public class PCListAdapter extends ArrayAdapter<PC> implements MyDialog.Listener {
/* ... */
public void showCommentDialog() {
FragmentManager fm = ((Activity)mContext).getFragmentManager();
MyDialog dialog = new MyDialog();
dialog.show(fm, "mydialog");
}
@Override
public void returnData(int result) {
}
}
Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment.
Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
The link you have read talks about communicating the Fragment with the Activity (using Listeners). This is done because the Fragment is tightly coupled to the Activity. Now in your case since you are using Adapter to launch a Fragment, this is you could probably do.
public class MyDialog extends DialogFragment {
private Listener mListener;
public void setListener(Listener listener) {
mListener = listener;
}
static interface Listener {
void returnData(int result);
}
/* ... */
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mBtnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.returnData(data);
}
dismiss();
}
});
}
}
and for Adapter,
public class PCListAdapter extends ArrayAdapter<PC> implements MyDialog.Listener {
/* ... */
public void showCommentDialog() {
FragmentManager fm = ((Activity)mContext).getFragmentManager();
MyDialog dialog = new MyDialog();
dialog.setListener(PCListAdapter.this);
dialog.show(fm, "mydialog");
}
@Override
public void returnData(int result) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With