Open up the
CaptureFragment.java
file in the editor.
We need to add a new member variable for the CaptureFragment
class to hold our new DataManager
class instance.
Add the new highlighted member variable to CaptureFragment
, which we have just discussed:
public class CaptureFragment extends Fragment{ private static final int CAMERA_REQUEST = 123; private ImageView mImageView; // Where the captured image is stored private Uri mImageUri = Uri.EMPTY; // Where to save the image String mCurrentPhotoPath; // A reference to our database private DataManager mDataManager; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); }
Now, we can initialize the DataManager
instance by adding this highlighted line of code to the onCreate
method in CaptureFragment
:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDataManager = new DataManager(getActivity() .getApplicationContext()); }
Since our DataManager
is coded and ready for use and we have an initialized instance of it, we can go ahead and implement the SAVE button. We already have a reference to all of the relevant EditText widgets as well as the Button widget via btnSave
.
We can finish the job of saving our data in onCreateView
by implementing an anonymous class to handle the button click. Inside the onClick
method, we step through the following tasks:
mImageUri
has been initialized with if(mImageUri != null)
.mImageUri
member is not empty with if (!mImageUri.equals(Uri.EMPTY))
.if
block and declare a new Photo
object.setTitle
and passing in the contents of the mEditTextTitle
EditText widget.setStorageLocation
and passing in mImageUri
.Photo
class to store them in the Photo
object.if
block is to use our DataManager
instance to call addPhoto,
and of course, we pass in our just-constructed Photo
object. The DataManager
class will add all the data into the required tables.if
block, we have an else
block that delivers a pop-up message to the user if he or she tries to save a photo that doesn't exist or if it is successful.Add the highlighted code in onCreateView
, which we have just discussed, right after the code that handles the CAPTURE button:
// Listen for clicks on the capture button btnCapture.setOnClickListener(new View.OnClickListener() { ... ... });// End capture button handling // Listen for clicks on the save button btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mImageUri != null){ if(!mImageUri.equals(Uri.EMPTY)) { // We have a photo to save Photo photo = new Photo(); photo.setTitle(mEditTextTitle.getText().toString()); photo.setStorageLocation(mImageUri); // What is in the tags String tag1 = mEditTextTag1.getText().toString(); String tag2 = mEditTextTag2.getText().toString(); String tag3 = mEditTextTag3.getText().toString(); // Assign the strings to the Photo object photo.setTag1(tag1); photo.setTag2(tag2); photo.setTag3(tag3); // Send the new object to our DataManager mDataManager.addPhoto(photo); Toast.makeText(getActivity(), "Saved", Toast.LENGTH_LONG).show(); }else { // No image Toast.makeText(getActivity(), "No image to save", Toast.LENGTH_LONG).show(); } }else { // Uri not initialized Log.e("Error ", "uri is null"); } } }); return view; }// End onCreateView
We could run the app at this point and actually begin to save photos to our database. The problem, of course, is that we don't yet have any way to see the list of available tags, photo titles, or any of the photos themselves.
Before TitlesFragment
can respond to clicks and show a photo, we need a fragment to show those photos. So, let's code a fragment to view photos.
18.116.63.5