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 } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "CREATE TABLE " + TABLE_NAME + "(" + BOOK_ID + " INTEGER primary key autoincrement, " + BOOK_NAME + " text " + BOOK_AUTHOR + "text);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String 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; import 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 mCursor; 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; @Override public 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 menu) { super.onCreateOptionsMenu(menu); menu.add(Menu.NONE, MENU_ADD, 0, "ADD"); menu.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 author = BookAuthor.getText().toString(); // 书名和作者都不能为空,或者退出 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(); // 书名和作者都不能为空,或者退出 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 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 context, Cursor 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 view, int position, long id) { // TODO Auto-generated method stub mCursor.moveToPosition(position); BOOK_ID = mCursor.getInt(0); BookName.setText(mCursor.getString(1)); BookAuthor.setText(mCursor.getString(2)); } } }