This time we continue to improve the application of criminal intent, make its function more perfect.
First we add a dialog box for criminal intent, which can either draw the user's attention or receive input from the user. Useful for prompting important information or providing user choice.
1: Create dialogfragment and add DatePicker to Alterdialog
@Override
Public Dialog Oncreatedialog (Bundle savedinstancestate) {
View v = layoutinflater.from (getactivity ())
. Inflate (r.layout.dialog_date, NULL);
return new Alertdialog.builder (Getactivity ())
. Setview (V)
. Settitle (R.string.date_picker_title)
. Setpositivebutton (Android. R.string.ok,null)
. Create ();
2: Show Dialogfragment
private static final String dialog_date = "Dialogdate";
Mdatebutton.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Fragmentmanager manager = Getfragmentmanager ();
Datepickerfragment Dialog = Datepickerfragment
. newinstance (Mcrime.getdate ());
Dialog.settargetfragment (Crimefragment.this, request_date);
Dialog.show (manager, Dialog_date);
}
});
3: Response DatePicker dialog box
public void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (resultcode! = ACTIVITY.RESULT_OK) {
Return
}
if (Requestcode = = request_date) {
Date date = (date) data
. Getserializableextra (Datepickerfragment.extra_date);
Mcrime.setdate (date);
Updatecrime ();
Updatedate ();
}
The second is the toolbar design. Toolbars can place menu options, provide app navigation, and help unify design styles and build brand image.
1: Response menu option
Need to be in crimelistfragment.
Implementing the Onoptionsitemselected (MenuItem) method
@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case R.id.new_crime:
Crime Crime = new Crime ();
Crimelab.get (Getactivity ()). Addcrime (crime);
UpdateUI ();
mcallbacks.oncrimeselected (crime);
return true;
Default
return super.onoptionsitemselected (item);
}
}
In the Crimelistfragment
Respond tothe Show subtitle menu item Click event
Case R.id.show_subtitle:
msubtitlevisible =!msubtitlevisible;
Getactivity (). Invalidateoptionsmenu ();
Updatesubtitle ();
return true;
Finally create the database
1: Create a comfortable database
Creating the Crimebasehelper Class
public class Crimebasehelper extends Sqliteopenhelper {
private static final int VERSION = 1;
private static final String database_name = "CRIMEBASE.DB";
Public Crimebasehelper (Context context) {
Super (context, database_name, NULL, VERSION);
}
@Override
public void OnCreate (Sqlitedatabase db) {
}
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
}
}
2: Writing to the database
Using Contentvalues
Values }
3: Read Database
Call Query (...) Method Query Record
Private Crimecursorwrapper Querycrimes (String whereclause, string[] whereargs) {
cursor cursor = Mdatabase.query (
Crimetable.name,
NULL,//Columns-null selects all Columns
Whereclause,
Whereargs,
NULL,//GroupBy
NULL,//having
Null //By
);
return new Crimecursorwrapper (cursor);
}
Android Development First Experience-criminalintent