1

FIRST: I don't need lessons on how creating and showing a Dialog, read all that I wrote please.

I have a problem and this is my first question here. I have an application with a Main Activity with a drawer on the left that when I click on an item it shows a Fragment in the main content. One Fragment let me add a planet in a SQLite Database. Then another Fragment shows me a list with the planets that I inserted in the database. Clicking on one of it, a FragmentDialog will appear and asks me if I want to delete the planet: if yes delete it, if not does nothing. The method onYes() seems to not being called, I think. Someone know why? This is the Class for the FragmentDialog:

public class FragmentDialog extends DialogFragment 
{   
    private IFragmentDialog mListener;

    public static FragmentDialog getInstance()
    {
        return new FragmentDialog();
}

   public interface IFragmentDialog
    {
        public void onYes();
        public void onNo();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {       
        AlertDialog.Builder vDialogBuilder = new AlertDialog.Builder(getActivity());
        vDialogBuilder.setTitle("Attenzione");
        vDialogBuilder.setMessage("Vuoi cancellare questo elemento?");
        vDialogBuilder.setPositiveButton("Sì", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                if (mListener != null)
                {
                    mListener.onYes();
                }
            }
        });

        vDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                if (mListener != null)
                {
                    mListener.onNo();
                }
            }
        });

        return vDialogBuilder.create();
    }

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        if (activity instanceof IFragmentDialog)
        {
            mListener = (IFragmentDialog) activity;
        }
    }
}

And this is the Class for the Fragment that will open the FragmentDialog:

public class FragmentPlanetList extends Fragment implements     LoaderManager.LoaderCallbacks<Cursor>, IFragmentDialog
{
    ListView mListView;
    PlanetCursorAdapter mCursorAdapter;
    private ILoadFragment mListener;

    public static Uri uri = null;

    private static final int PLANET_LOADER_ID = 0;

    public static FragmentPlanetList getInstance()
{
    return new FragmentPlanetList();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View vView = inflater.inflate(R.layout.fragment_planet_list, container, false);
    getActivity().setTitle(R.string.fragment_planet_list);

    mListView = (ListView) vView.findViewById(R.id.planet_list);

    mCursorAdapter = new PlanetCursorAdapter(getActivity().getApplicationContext(), null, 0);

    mListView.setAdapter(mCursorAdapter);

    getLoaderManager().initLoader(PLANET_LOADER_ID, null, this);

    mListView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
            uri = Uri.parse(PlanetProvider.PLANETS_URI + "/" + id);

// getActivity().getContentResolver().delete(uri, null, null); Toast.makeText(getActivity(), "Cancellato ID " + id, Toast.LENGTH_SHORT).show();

            showDialog();
        }
    });

    return vView;
}

protected void showDialog() 
{
    FragmentDialog vDialog = FragmentDialog.getInstance();
    vDialog.show(getFragmentManager(), "SHOW_DIALOG");
}

@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    if (activity instanceof ILoadFragment)
    {
        mListener = (ILoadFragment) activity;
    }
}

    @Override
    public void onYes() 
    {
        // not called
        Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG).show();

// getActivity().getContentResolver().delete(uri, null, null); }

    @Override
    public void onNo() 
    {

    }
}

EDIT: I think that the problem is (like OrhanC1 said) the if in onAttach() method. Have I to implement the FragmentDialog interface also in my Activity? If so, how can I pass the position of the list item and the uri to delete?

Community
  • 1
  • 1
let92
  • 11
  • 2
  • 2
    Consider trimming your code down to the shortest sample you can come up with that reproduces the problem. Two good things will happen: 1) People will be more likely to read all of it, and 2) the exercise of creating a short example may very well lead you to find the problem. – Dale Wilson Jul 07 '14 at 16:23
  • I think `if (activity instanceof ILoadFragment)` is causing the issue. Please add a `Log.d("test", "In IF");` inside the if statement and check logcat – OrhanC1 Jul 07 '14 at 16:26
  • There isn't the Log.d in the Logcat. – let92 Jul 07 '14 at 16:37
  • if you simply want to show a dialog with yes and no option . See i here http://stackoverflow.com/questions/11111796/android-show-custom-dialog/24573658#24573658 – Zar E Ahmer Jul 08 '14 at 06:00
  • The Dialog is showing, but the methods inside the interface aren't called. – let92 Jul 08 '14 at 06:27
  • i think you should implement IFragmentDialog in FragmentDialog fragment. then these methods will be invoke. for them – Zar E Ahmer Jul 08 '14 at 06:33
  • I learned that the hosting Activity/Fragment must implement the Interface, not the Dialog, right? – let92 Jul 08 '14 at 06:37
  • This link may help http://sunil-android.blogspot.com/2013/08/dialogfragment-in-android.html – Zar E Ahmer Jul 08 '14 at 07:54
  • I edited the question, please read carefully. The problem is not showing the Dialog, but calling the methods. The methods aren't calling! – let92 Jul 08 '14 at 08:02

2 Answers2

0

Create a DialogHanlder

public class DialogHandler {
    public Runnable ans_true = null;
    public Runnable ans_false = null;

    // Dialog. --------------------------------------------------------------

    public boolean Confirm(Activity act, String Title, String ConfirmText,
            String CancelBtn, String OkBtn, Runnable aProcedure, Runnable bProcedure) {
        ans_true = aProcedure;
        ans_false= bProcedure;
        AlertDialog dialog = new AlertDialog.Builder(act).create();
        dialog.setTitle(Title);
        dialog.setMessage(ConfirmText);
        dialog.setCancelable(false);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int buttonId) {
                         ans_true.run();
                    }
                });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int buttonId) {
                        ans_false.run();
                    }
                });
        dialog.setIcon(android.R.drawable.ic_dialog_alert);
        dialog.show();
        return true;
    }
}

Way to call it another Activity

public class YourActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(myclick);
    }

    public final Button.OnClickListener myclick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            doclick();
        }
    };

    public void doclick() {
        DialogHandler appdialog = new DialogHandler();
        boolean dlg = appdialog.Confirm(this, "Message title", "Message content",
                "Cancel", "OK", aproc(), bproc());
    }

    public Runnable aproc(){
        return new Runnable() {
            public void run() {
                Log.d("Test", "This from A proc");
            }
          };
    }

    public Runnable bproc(){
        return new Runnable() {
            public void run() {
                Log.d("Test", "This from B proc");
            }
          };
    }


}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • instead of appdialog.Confirm(this . you have to pass appdialog.Confirm(getActivity while using Fragments – Zar E Ahmer Jul 08 '14 at 07:24
  • I need using a FragmentDialog, not another type of Dialog. Furthermore, I use only one Activity and some Fragments in my application. – let92 Jul 08 '14 at 07:35
  • why you only need to use FragmentDialog whereas this dialog can also do what you need – Zar E Ahmer Jul 08 '14 at 07:38
  • Because FragmentDialog remain active also if I rotate the device, and because my teacher told me that FragmentDialogs are better for programming. – let92 Jul 08 '14 at 07:41
0

A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.

Create your Custom Dialog with any number of button in onCreateDialog(); In the given code ok is disable you can enable it by copying the code as the setNegativeButton Do.

Replace null with new DialogInterface blah blah . if you like

Heres an example DialogFragment:

class MyDialogFragment extends DialogFragment{
    Context mContext;
    public MyDialogFragment() {
        mContext = getActivity();
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setTitle("Really?");
        alertDialogBuilder.setMessage("Are you sure?");
        //null should be your on click listener
        alertDialogBuilder.setPositiveButton("OK", null);
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });


        return alertDialogBuilder.create();
    }
}

To create the dialog call:

new MyDialogFragment().show(getFragmentManager(), "MyDialog");

And to dismiss the dialog from somewhere:

((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();

All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • I edited the question, please read carefully. The problem is not showing the Dialog, but calling the methods. The methods aren't calling! – let92 Jul 08 '14 at 08:01