Coding the Fragment classes to use the DataManager
Add this highlighted code to the InsertFragment
class to update the onCreateView
method as follows:
View v = inflater.inflate(R.layout.content_insert, container, false); final DataManager dm = new DataManager(getActivity()); Button btnInsert = (Button) v.findViewById(R.id.btnInsert); final EditText editName = (EditText) v.findViewById(R.id.editName); final EditText editAge = (EditText) v.findViewById(R.id.editAge); btnInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dm.insert(editName.getText().toString(), editAge.getText().toString()); } }); return v;
In the code, we get an instance of our DataManager
class and a reference to each of our UI widgets. Then, in the onClick
method of the button, we use the insert method to add a new name and age to the database. The values to insert are taken from the two EditText
widgets...