1, the dialog box added
Add a dialog box for a previous app
Tasks that need to be completed:
1) Create Datepickerfragment
@Override
Public Dialog Oncreatedialog (Bundle savedinstancestate) {
return new Alertdialog.builder (Getactivity ())
. Setview (V)
. Settitle (R.string.date_picker_title)
. Setpositivebutton (Android. R.string.ok,null)
. Create ();
}
2) Display dialog box with Fragmentmanager
To pass crime to datepickerfragment need to create a new
Newinstance (Date) method:
public class Datepickerfragment extends Dialogfragment {
private static final String arg_date = "DATE";
Private DatePicker Mdatepicker;
public static datepickerfragment newinstance (date date) {
Bundle args = new bundle ();
Args.putserializable (Arg_date, DATE);
Datepickerfragment fragment = new Datepickerfragment ();
Fragment.setarguments (args);
return fragment;
}
Also add a string to the String.xml:
<string name= "Date_picker_title" >date of crime:</string>
Responses to dialog boxes:
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 ();
}
2. Toolbars and menus
1) Generate icon:
Right-click the drawable directory and select New→image Asset
Click Next to enter the preview screen:
2) Response menu item selection
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
Response SHOW SUBTITLE menu item Click event
Case R.id.show_subtitle:
msubtitlevisible =!msubtitlevisible;
Getactivity (). Invalidateoptionsmenu ();
Updatesubtitle ();
return true;
3. SQLite database
1) Create the initial database
Create the Crimebasehelper class with the following code:
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) write SQL to create the initial code
Db.execsql ("CREATE TABLE" + Crimetable.name + "(" +
"_id Integer primary Key autoincrement," +
CrimeTable.Cols.UUID + "," +
CrimeTable.Cols.TITLE + "," +
CrimeTable.Cols.DATE + "," +
CrimeTable.Cols.SOLVED + "," +
CrimeTable.Cols.SUSPECT +
")"
);
The code needs to import crimetable, in the word crimetable,
Press ALT + ENTER to select the Prompt option
3) Read the 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);
}
SQLite database Use