Returning data from a DialogFragment to an Adapter
Asked Answered
G

1

11

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) {
    }
}
Generable answered 2/6, 2013 at 18:22 Comment(0)
C
17

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) {
}
}
Conjurer answered 2/6, 2013 at 18:52 Comment(5)
This doesn't work, the line dialog.setListener(this) is invalid.Olivenite
I have exactly the same problem with error at dialog.setListener(this). Any solution?Kiona
actually I solved it now. Just don't put this line directly inside mBtnSubmit.setOnClickListenerKiona
Your answer saved me lot of hassleSchaaf
@Olivenite @John you can you dialog.setListener(ClassName.this);Yeomanry

© 2022 - 2024 — McMap. All rights reserved.