This log is generated by learning a Big Brother's code. At that time, he looked at his code and was confused. It took a long time to copy and debug the code. Original article see: http://blog.csdn.net/Android_Tutor/article/details/5654124
The modified code is as follows:
Main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><EditText android:id="@+id/bookname" android:layout_width="fill_parent" android:layout_height="wrap_content" /><EditText android:id="@+id/author" android:layout_width="fill_parent" android:layout_height="wrap_content" /><ListView android:id="@+id/booklist" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ListView></LinearLayout>
Books_db.java
package com.wangxin;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class BooksDB extends SQLiteOpenHelper {private final static String DATABASE_NAME = "BOOKS.db";private final static int DATABASE_VERSION = 1;public final static String TABLE_NAME = "books_table";public final static String BOOK_ID = "book_id";public final static String BOOK_NAME = "book_name";public final static String BOOK_AUTHOR = "book_author";public BooksDB(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}public BooksDB(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubString sql = "CREATE TABLE " + TABLE_NAME + "(" + BOOK_ID+ " INTEGER primary key autoincrement, " + BOOK_NAME + " text "+ BOOK_AUTHOR + "text);";db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubString sql = "DROP TABLE IF EXISTS" + TABLE_NAME;db.execSQL(sql);onCreate(db);}public Cursor select() {SQLiteDatabase db = this.getReadableDatabase();Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);return cursor;}public long insert(String bookname, String author) {SQLiteDatabase db = this.getWritableDatabase();ContentValues cv = new ContentValues();cv.put(BOOK_NAME, bookname);cv.put(BOOK_AUTHOR, author);long row = db.insert(TABLE_NAME, null, cv);return row;}public void delete(int id) {SQLiteDatabase db = this.getWritableDatabase();String where = BOOK_ID + "=?";String[] whereValue = { Integer.toString(id) };db.delete(TABLE_NAME, where, whereValue);}public void update(int id, String bookname, String author) {SQLiteDatabase db = this.getWritableDatabase();String where = BOOK_ID + "=?";String[] whereValue = { Integer.toString(id) };ContentValues cv = new ContentValues();cv.put(BOOK_NAME, bookname);cv.put("BOOK_AHTHOR", author);db.update(TABLE_NAME, cv, where, whereValue);}}
Sqlitedemoactivity. Java
Package COM. wangxin; import android. app. activity; import android. content. context; import android. database. cursor; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. edittext; impo RT android. widget. listadapter; import android. widget. listview; import android. widget. simpleadapter; import android. widget. textview; import android. widget. toast; public class sqlitedemoactivity extends activity {/** called when the activity is first created. */private edittext bookname = NULL; private edittext bookauthor = NULL; private listview bookslist = NULL; private booksdb mbooksdb; private cursor MCU Rsor; private int book_id = 0; protected final static int menu_add = menu. first; protected final static int menu_delete = menu. first + 1; protected final static int menu_update = menu. first + 2; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); setupviews ();} public void setupviews () {mbooksdb = new booksdb (this); mcursor = mbooksdb. Select (); bookname = (edittext) findviewbyid (R. id. bookname); bookauthor = (edittext) findviewbyid (R. id. author); bookslist = (listview) findviewbyid (R. id. booklist); bookslist. setadapter (New bookslistadapter (this, mcursor); bookslist. setonitemclicklistener (New onitemclicklistener ();} public Boolean oncreateoptionsmenu (menu) {super. oncreateoptionsmenu (menu); menu. add (menu. none, menu_add, 0, "add"); m Enu. add (menu. none, menu_delete, 0, "delete"); menu. add (menu. none, menu_delete, 0, "Update"); Return true;} public Boolean onoptionsitemselected (menuitem item) {super. onoptionsitemselected (item); Switch (item. getitemid () {Case menu_add: add (); break; Case menu_delete: delete (); break; Case menu_update: Update (); break;} return true ;} public void add () {string bookname = bookname. gettext (). tostring (); string auth OR = bookauthor. gettext (). tostring (); // neither the title nor the author can be blank, or exit if (bookname. equals ("") | author. equals ("") {return;} mbooksdb. insert (bookname, author); mcursor. requery (); bookslist. invalidateviews (); bookname. settext (""); bookauthor. settext (""); toast. maketext (this, "add successed! ", Toast. length_short ). show () ;}public void Delete () {If (book_id = 0) {return;} mbooksdb. delete (book_id); mcursor. requery (); bookslist. invalidateviews (); bookname. settext (""); bookauthor. settext (""); toast. maketext (this, "delete successed! ", Toast. length_short ). show ();} public void Update () {string bookname = bookname. gettext (). tostring (); string author = bookauthor. gettext (). tostring (); // neither the title nor the author can be blank, or exit if (bookname. equals ("") | author. equals ("") {return;} mbooksdb. update (book_id, bookname, author); mcursor. requery (); bookslist. invalidateviews (); bookname. settext (""); bookauthor. settext (""); toast. maketext (this, "Update successed! ", Toast. length_short). Show ();}/* Public void onitemclicked (adapterview <?> Parent, view, int position, long ID) {mcursor. movetoposition (position); book_id = mcursor. getint (0); bookname. settext (mcursor. getstring (1); bookauthor. settext (mcursor. getstring (2);} */public class bookslistadapter extends baseadapter {private context mcontext; private cursor mcursor; Public bookslistadapter (context, cursor) {mcontext = context; mcursor = cursor;} public int getcount () {Return mcursor. getcount ();} public object getitem (INT position) {return NULL;} public long getitemid (INT position) {return 0;} public view getview (INT position, view convertview, viewgroup parent) {textview mtextview = new textview (mcontext); mcursor. movetoposition (position); mtextview. settext (mcursor. getstring (1) + "___" + mcursor. getstring (2); Return mtextview;} class onitemclicklistener implements Onitemclicklistener {public void onitemclick (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stubmcursor. movetoposition (position); book_id = mcursor. getint (0); bookname. settext (mcursor. getstring (1); bookauthor. settext (mcursor. getstring (2 ));}}}