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?