Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute action after DialogFragment positive button clicked

I created the following DialogFragment deriving it from the Android documentation:

public class PayBillDialogFragment extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        final Bundle b = this.getArguments();
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Paga bollettino")
               .setPositiveButton("Paga", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!


                   }
               })
               .setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();

    }





}

From another Fragment (a ListFragment), when a row of the list is clicked the DialogFragment should be opened and after pressing the positive button of the DialogFragment I want to be able to remove the selected row of the ListFragment and also to call a method to perform a remote action associated to the removal. I implemented the ListFragment as follows:

public static class ListFragment extends android.support.v4.app.ListFragment {



        ArrayList<String> listItems=new ArrayList<String>();


        ArrayAdapter<String> adapter;


        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.list_fragment_view,
                    container, false);


            ListView lv = (ListView)rootView.findViewById(android.R.id.list);

            }});
            adapter=new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_list_item_1,
                    listItems);
                setListAdapter(adapter);
            return rootView;
        }



        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {



            //opening the dialogfragment


        }


    }




    }

What I don't know is how to handle the action after the click of the positive button of the DialogFragment. Can you help me?

EDIT: to clarify, this is the workflow: click on the list -> show the DialogFragment -> after click on DialogFragment remove the element from the list.

like image 457
Raffo Avatar asked Apr 12 '13 11:04

Raffo


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. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

How do you prevent a dialog from closing when a button is clicked?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);

How do I know if DialogFragment is showing?

This is gonna be disappointing to you "great" finding but just call progressDialog. showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.

Which of the following OnClickListener is use to implement click events of alert dialog?

onClickListener() for each button, you can let the class implement AlertDialog. OnClickListener , and then override public void onClick(DialogInterface dialog, int which) . The int which in this method is the button that was clicked, or the position.


1 Answers

This is how I handle communication between fragment and dialog fragment

Example fragment:

public class MainFragment extends Fragment {

    private static final int REQ_CODE = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment, container, false);
        Button b = (Button) v.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyDialog dialog = new MyDialog();
                dialog.setTargetFragment(MainFragment.this, REQ_CODE);
                dialog.show(getFragmentManager(), "dialog");
            }
        });
        return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(getActivity(), "Result: " + resultCode,
                Toast.LENGTH_SHORT).show();
        super.onActivityResult(requestCode, resultCode, data);
    }

}

Example DialogFragment:

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("My dialog message")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        notifyToTarget(Activity.RESULT_OK);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                notifyToTarget(Activity.RESULT_CANCELED);
                            }
                        });
        return builder.create();
    }

    private void notifyToTarget(int code) {
        Fragment targetFragment = getTargetFragment();
        if (targetFragment != null) {
            targetFragment.onActivityResult(getTargetRequestCode(), code, null);
        }
    }

}

This is the only method I got working also when changing orientation.

like image 137
rciovati Avatar answered Sep 22 '22 21:09

rciovati