Question: Having issues with steps 5-7 in an Android Studio assignment below: Create your Details Fragment: Next will will be adding a blank Fragment that will
Having issues with steps 5-7 in an Android Studio assignment below:
Create your Details Fragment: Next will will be adding a blank Fragment that will contain the details UI to our FragmentActivity. This will allow your app to display details about the products, members, events or todo's selected from the ListFragment you already created. To create your DetailsFragment right-click in the folder where you want to generate your fragment (e.g. edu.ucdenver.myproject) and select "New -> Fragment (Blank)."
1. When setting up the Fragment give it a meaningful name like ProductDetailsFragment, MemberDetailsFragment, ItemDetailsFragment.
2. Uncheck "Include fragment factory methods" but leave the XML layout and Interface callback items checked.
3. This will create a new class that extends Fragment. a. Make sure the include file at the top is: include android.app.Fragment - NOT include android.support.v4.app.Fragment (or some other alternative Fragment package).
4. The auto-generated fragment file should look something like: DetailsFragment.java
(if it does not then copy the code from DetailsFragment.java
into your class).
5. You will want to create 3 private global variables to hold the data you pass to the fragment (e.g. a String, and int and a boolean).
6. In the onCreate method add a getArguments if statement to check to see if arguments are being passed to the DetailsFragment (if (getArguments() != null) {}). Inside this if statement you will call the getArguments method 3x. Passing it the three names you used in the putExtras statement you used when creating your intent (above) and assign the values returned to the three global variables.
7. You can set the title to the id (for the time being) or to the String data that was passed in (again like the last assignment).
here is my current code for my ContactDetailsFragment.java:
package edu.ucdenver.contactlistultimate; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link ContactDetailsFragment.OnFragmentInteractionListener} interface * to handle interaction events. */ public class ContactDetailsFragment extends Fragment { private OnFragmentInteractionListener mListener; private String string; //I created these for step 5, is this correct? private int int1; //I created these for step 5, is this correct? private boolean boolean1; //I created these for step 5, is this correct? public ContactDetailsFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(getArguments() != null) { getArguments().getString(string); getArguments().getInt(int1); getArguments().getBoolean(boolean1) //This is as far as I've gotten for step 6 } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_contact_details, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * * See the Android Training lesson * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
