Main Code:
Package EOE. Demo;
Import Android. App. activity;
Import Android. database. cursor;
Import Android. database. SQLite. sqlitecursor;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. edittext;
Import Android. widget. listview;
Import Android. widget. simplecursoradapter;
Public class dbactivity extends activity {
Private tododb mytododb;
Private cursor mycursor;
Private listview mylistview;
Private edittext myedittext;
Private int _ id;
Protected final static int menu_add = menu. first;
Protected final static int menu_edit = menu. First + 1;
Protected final static int menu_delete = menu. First + 2;
Public Boolean onoptionsitemselected (menuitem item)
{
Super. onoptionsitemselected (item );
Switch (item. getitemid ())
{
Case menu_add:
This. addtodo ();
Break;
Case menu_edit:
This. edittodo ();
Break;
Case menu_delete:
This. deletetodo ();
Break;
}
Return true;
}
Public Boolean oncreateoptionsmenu (menu)
{
Super. oncreateoptionsmenu (menu );
Menu. Add (menu. None, menu_add, 0, R. String. straddbutton );
Menu. Add (menu. None, menu_edit, 0, R. String. streditbutton );
Menu. Add (menu. None, menu_delete, 0, R. String. strdeletebutton );
Return true;
}
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mylistview = (listview) This. findviewbyid (R. Id. mylistview );
Myedittext = (edittext) This. findviewbyid (R. Id. myedittext );
Mytododb = new tododb (this );
/* Obtain data in the database */
Mycursor = mytododb. Select ();
/* New simplecursoradapter and input mycursor,
The data field is todo_text */
Simplecursoradapter adapter =
New simplecursoradapter
(This, R. layout. List, mycursor, new string []
{Tododb. field_text}, new int []
{R. Id. listtextview1 });
Mylistview. setadapter (adapter );
/* Add onitemclicklistener to mylistview */
Mylistview. setonitemclicklistener
(New adapterview. onitemclicklistener ()
{
Public void onitemclick
(Adapterview <?> Arg0, view arg1, int arg2, long arg3)
{
/* Move mycursor to the clicked value */
Mycursor. movetoposition (arg2 );
/* Obtain the value of field _ id */
_ Id = mycursor. getint (0 );
/* Obtain the value of the todo_text field */
Myedittext. settext (mycursor. getstring (1 ));
}
});
Mylistview. setonitemselectedlistener
(New adapterview. onitemselectedlistener ()
{
Public void onitemselected
(Adapterview <?> Arg0, view arg1, int arg2, long arg3)
{
/* Getselecteditem obtains sqlitecursor */
Sqlitecursor SC = (sqlitecursor) arg0.getselecteditem ();
_ Id = SC. getint (0 );
Myedittext. settext (SC. getstring (1 ));
}
Public void onnothingselected (adapterview <?> Arg0)
{
}
});
}
Private void addtodo ()
{
If (myedittext. gettext (). tostring (). Equals (""))
Return;
/* Add data to the database */
Mytododb. insert (myedittext. gettext (). tostring ());
/* Re-query */
Mycursor. requery ();
/* Refresh mylistview */
Mylistview. invalidateviews ();
Myedittext. settext ("");
_ Id = 0;
}
Private void edittodo ()
{
If (myedittext. gettext (). tostring (). Equals (""))
Return;
/* Modify data */
Mytododb. Update (_ id, myedittext. gettext (). tostring ());
Mycursor. requery ();
Mylistview. invalidateviews ();
Myedittext. settext ("");
_ Id = 0;
}
Private void deletetodo ()
{
If (_ id = 0)
Return;
/* Delete data */
Mytododb. Delete (_ id );
Mycursor. requery ();
Mylistview. invalidateviews ();
Myedittext. settext ("");
_ Id = 0;
}
}
: