Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning data from a DialogFragment to an Adapter

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) {
    }
}
like image 360
jul Avatar asked Jun 02 '13 18:06

jul


People also ask

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment.

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

What is the difference between dialog & DialogFragment?

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.


1 Answers

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) {
}
}
like image 112
Sagar Waghmare Avatar answered Oct 26 '22 03:10

Sagar Waghmare