AddEditFragment Overridden Method onCreateView

In method onCreateView (Fig. 8.38), lines 70–78 inflate the GUI and get the Fragment’s EditTexts. Next, we use Fragment method getArguments to get the Bundle of arguments (if any). When we launch the AddEditFragment from the MainActivity, we don’t pass a Bundle, because the user is adding a new contact’s information. In this case, getArguments will return null. If it returns a Bundle (line 82), then the AddEditFragment was launched from the DetailsFragment and the user chose to edit an existing contact. Lines 84–91 read the arguments out of the Bundle by calling methods getLong (line 84) and getString, and the String data is displayed in the EditTexts for editing. Lines 95–97 register a listener (Fig. 8.39) for the Save Contact Button.


60      // called when Fragment's view needs to be created
61      @Override
62      public View onCreateView(LayoutInflater inflater, ViewGroup container,
63         Bundle savedInstanceState)
64      {
65         super.onCreateView(inflater, container, savedInstanceState);
66         setRetainInstance(true); // save fragment across config changes
67         setHasOptionsMenu(true); // fragment has menu items to display
68
69         // inflate GUI and get references to EditTexts
70         View view =
71            inflater.inflate(R.layout.fragment_add_edit, container, false);
72         nameEditText = (EditText) view.findViewById(R.id.nameEditText);
73         phoneEditText = (EditText) view.findViewById(R.id.phoneEditText );
74         emailEditText = (EditText) view.findViewById(R.id.emailEditText );
75         streetEditText = (EditText) view.findViewById(R.id.streetEditText );
76         cityEditText = (EditText) view.findViewById(R.id.cityEditText);
77         stateEditText = (EditText) view.findViewById(R.id.stateEditText );
78         zipEditText = (EditText) view.findViewById(R.id.zipEditText );
79
80         contactInfoBundle = getArguments(); // null if creating new contact
81
82         if (contactInfoBundle != null)
83         {
84            rowID = contactInfoBundle.getLong(MainActivity.ROW_ID );
85            nameEditText.setText(contactInfoBundle.getString("name" ));
86            phoneEditText.setText(contactInfoBundle.getString("phone"));
87            emailEditText.setText(contactInfoBundle.getString("email"));
88            streetEditText.setText(contactInfoBundle.getString("street" ));
89            cityEditText.setText(contactInfoBundle.getString("city" ));
90            stateEditText.setText(contactInfoBundle.getString("state"));
91            zipEditText.setText(contactInfoBundle.getString("zip"));
92         }
93
94         // set Save Contact Button's event listener
95         Button saveContactButton =
96            (Button) view.findViewById(R.id.saveContactButton);
97         saveContactButton.setOnClickListener(saveContactButtonClicked);
98         return view;
99      }
100


Fig. 8.38 | AddEditFragment overridden method onCreateView.


101      // responds to event generated when user saves a contact
102      OnClickListener saveContactButtonClicked = new OnClickListener()
103      {
104         @Override
105         public void onClick(View v)
106         {
107            if ( nameEditText.getText().toString().trim().length() != 0)
108            {
109               // AsyncTask to save contact, then notify listener
110               AsyncTask<Object, Object, Object> saveContactTask =
111                  new AsyncTask<Object, Object, Object>()
112                  {
113                     @Override
114                     protected Object doInBackground(Object... params)
115                     {
116                        saveContact(); // save contact to the database
117                        return null;
118                     }
119
120                     @Override
121                     protected void onPostExecute(Object result)
122                     {
123                        // hide soft keyboard
124                        InputMethodManager imm = (InputMethodManager)
125                           getActivity().getSystemService(
126                              Context.INPUT_METHOD_SERVICE);
127                        imm.hideSoftInputFromWindow(
128                           getView().getWindowToken(), 0);
129
130                        listener.onAddEditCompleted(rowID);
131                     }
132                  }; // end AsyncTask
133
134               // save the contact to the database using a separate thread
135               saveContactTask.execute((Object[]) null);
136            }
137            else // required contact name is blank, so display error dialog
138            {
139               DialogFragment errorSaving =
140                  new DialogFragment()
141                  {
142                    @Override
143                    public Dialog onCreateDialog(Bundle savedInstanceState)
144                    {
145                       AlertDialog.Builder builder =
146                          new AlertDialog.Builder(getActivity());
147                       builder.setMessage(R.string.error_message);
148                       builder.setPositiveButton(R.string.ok, null);
149                       return builder.create();
150                    }
151                  };
152
153                    errorSaving.show(getFragmentManager(), "error saving contact");
154            }
155         } // end method onClick
156      }; // end OnClickListener saveContactButtonClicked
157


Fig. 8.39 | OnClickListener to process Save Contact Button events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset