How to receive the actions made by the user in a DialogFragment?

萝らか妹 提交于 2019-12-13 04:20:56

问题


I have a ListView that displays a list of Contacts. When a contact is clicked, a DialogFragment is created which displays a notice on whether or not to Cancel or Confirm. Based on whether they click Cancel or Confirm I want to change the color of the ListView item. How can I get the status of the Button click for the dialog. My code for the dialog:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        String first = this.getArguments().getString("first");
        String last = this.getArguments().getString("last");
        String phone = this.getArguments().getString("phone");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.add_contact_dialog, null);
        TextView tv = (TextView) view.findViewById(R.id.new_first);
        tv.setText(first);

        TextView tv2 = (TextView) view.findViewById(R.id.new_phone);
        tv2.setText(phone);

        TextView tv3 = (TextView) view.findViewById(R.id.new_last);
        tv3.setText(last);

        builder.setView(view);
        //builder.setTitle("Are you sure?").setMessage("Would you like to add " + name + " as a contact?");

        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Add the contact

                   }
               })
               .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });

        // Create the AlertDialog object and return it
        return builder.create();
    }

Now I want setPositiveButton or setNegativeButton to return a value that I can compare with in my Main Activity or get my ArrayAdapter in the scope of the dialog so that I can make changes to it. Is there any way to relate the DialogFragment.show() and the Dialog button that was clicked?


回答1:


Is there any way to relate the DialogFragment.show() and the Dialog button that was clicked?

Use an interface to pass that information to the activity which in turn will pass it to the adapter/fragment that has the possibility to do the change:

public interface OnSelectionMade {

     int OK = 1000;
     int CANCEL = 2000;
     doChange(int result);
}

Make the activity implement this interface and use the doChange() callback to do the change in the adapter, either by accessing the adapter directly(if you use a simple ListView) or pass it to the fragment holding the ListView to do the change:

public class YourActivity extends Activity implements OnSelectionMade {

     @Override
     public void doChange(int result) {
         // the result parameter will be either the OK or CANCEL constants
         // you know now what button was clicked so you can do the change
         ...
     }

}

Now you need to wire up the DialogFragment to pass the event through OnSelectionMade like this:

private OnSelectionMade mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mListener = (OnSelectionMade) activity; // you'd want to implement a cast check here to be safe
}

//then in the onCreateDialog() method
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.OK); // do a null mListener check?
                   }
               })
               .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.CANCEL);
                   }
               });

You could probably pass a reference to the DialogFragment directly to pass the results, but that is not a very good solution long term.




回答2:


Indeed there is. In the Activity or Fragment from where you are instantiating your DialogFragment, first declare:

public static final int MY_DIALOGFRAGMENT = 123;   /* Any random int values will do. */
public static final int CONTACT_CONFIRM = 124;
public static final int CONTACT_DENY = 125;

Instantiate your DialogFragment like so:

FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.setTargetFragment(this, MY_DIALOGFRAGMENT);
myDialogFragment.show(fm, "my_dialog_fragment");

In the onCreateDialog() method of your DialogFragment,

builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Add the contact
                   getTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_CONFIRM, getActivity().getIntent());
                   dismiss();
               }
           })
           .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
                   getTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_DENY, getActivity().getIntent());
                   dismiss();
               }
           });

And finally, add the following method to your Activity or Fragment:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case MY_DIALOGFRAGMENT:

                if (resultCode == CONTACT_CONFIRM) {

                    // Change color of listview item to green

                } else if (resultCode == CONTACT_DENY){

                    // Change color of listview item to red

                }

                break;
        }
}

This should work. Try it.



来源:https://stackoverflow.com/questions/24031167/how-to-receive-the-actions-made-by-the-user-in-a-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!