Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement ItemViewDialogFragment

It listens for edit and delete actions.

Signed off by me
  • Loading branch information...
commit 00ef1c1ba955597bfee7b51a7a4e59d164c2eb56 1 parent 2ae6ee8
@simonewebdesign authored
View
42 src/com/example/todolist/ItemViewDialogFragment.java
@@ -1,5 +1,6 @@
package com.example.todolist;
+import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
@@ -10,6 +11,9 @@
public class ItemViewDialogFragment extends DialogFragment {
private final String TAG = "ItemViewDialogFragment";
+ // Use this instance of the interface to deliver action events
+ private ItemViewDialogListener mListener;
+
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
@@ -20,11 +24,47 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
- Log.v(TAG, "ITS OVER 9000!!!!!!!!!!!!!!11");
+ Log.v(TAG, "an action has been selected");
+
+ // Send the positive button event back to the host activity
+ if (which == 0) {
+ mListener.onDialogEditClick(ItemViewDialogFragment.this);
+ } else
+ if (which == 1) {
+ mListener.onDialogDeleteClick(ItemViewDialogFragment.this);
+ }
}
});
// Create the AlertDialog object and return it
return builder.create();
}
+
+ /* The activity that creates an instance of this dialog fragment must
+ * implement this interface in order to receive event callbacks.
+ * Each method passes the DialogFragment in case the host needs to query it. */
+ public interface ItemViewDialogListener {
+ public void onDialogEditClick(DialogFragment dialog);
+ public void onDialogDeleteClick(DialogFragment dialog);
+ }
+
+
+
+ // Override the Fragment.onAttach() method to instantiate the ItemViewDialogListener, which is an interface.
+ @Override
+ public void onAttach(Activity activity) {
+ super.onAttach(activity);
+ // Verify that the host activity implements the callback interface
+ try {
+ // Instantiate the NoticeDialogListener so we can send events to the host
+ mListener = (ItemViewDialogListener) activity;
+ } catch (ClassCastException e) {
+ // The activity doesn't implement the interface, throw exception
+ throw new ClassCastException(activity.toString()
+ + " must implement ItemViewDialogListener");
+ }
+ }
+
+
+
}
View
15 src/com/example/todolist/MainActivity.java
@@ -12,6 +12,7 @@
import android.os.Bundle;
import android.content.Context;
+import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
@@ -20,7 +21,7 @@
import android.widget.EditText;
import android.widget.ListView;
-public class MainActivity extends FragmentActivity {
+public class MainActivity extends FragmentActivity implements ItemViewDialogFragment.ItemViewDialogListener {
private static final String TAG = "MainActivity";
private Context context = null;
@@ -224,4 +225,16 @@ public void saveData() {
e.printStackTrace();
}
}
+
+ @Override
+ public void onDialogEditClick(DialogFragment dialog) {
+ // TODO Auto-generated method stub
+ Log.v(TAG, "onDialogEditClick");
+ }
+
+ @Override
+ public void onDialogDeleteClick(DialogFragment dialog) {
+ // TODO Auto-generated method stub
+ Log.v(TAG, "onDialogDeleteClick");
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.