Skip to content

Instantly share code, notes, and snippets.

@yareally
Last active December 29, 2015 21:09
Show Gist options
  • Save yareally/7728570 to your computer and use it in GitHub Desktop.
Save yareally/7728570 to your computer and use it in GitHub Desktop.
package com.cc.app.fragments;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.cc.app.R;
import com.cc.app.db.DatabaseHelper;
/**
* @author Wes Lanning
* @version 2013-08-31
*/
public class AddItemDialogFragment extends DialogFragment
implements DialogInterface.OnShowListener,
DialogInterface.OnClickListener,
EditText.OnFocusChangeListener
{
private ItemInsertedListener listener;
private View form = null;
private DatabaseHelper databaseHelper;
public AddItemDialogFragment(ItemInsertedListener listener)
{
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
databaseHelper = DatabaseHelper.getInstance(getActivity());
// code omitted
return ad;
}
/**
* This method will be invoked when a button in the dialog is clicked.
*
* @param dialog The dialog that received the click.
* @param which The button that was clicked (e.g.
* {@link android.content.DialogInterface#BUTTON1}) or the position
* of the item clicked.
*/
@Override
public void onClick(DialogInterface dialog, int which)
{
String item = ((TextView) form.findViewById(R.id.item_text)).getText().toString().trim();
databaseHelper.addItem(item);
listener.notifyInsertion(item);
}
// some methods ommitted that aren't relevant...
/**
* Listener to tell the fragment that we updated the list
* item's state changed and this passes back the altered item.
*
* Needed so the fragment can then updated it in the database.
*/
public interface ItemInsertedListener
{
void notifyInsertion(String item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment