实现效果图:
(1)activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bookname"> </EditText> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bookauthor"> </EditText> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/booklist"> </ListView> </LinearLayout>
(3)MainActivity
public class MainActivity extends AppCompatActivity implements OnItemClickListener{ private BookDB mBookDB; private Cursor mCursor; private EditText bookName; private EditText bookAuthor; private ListView bookList; private int BOOK_ID=0; private static final int MENU_ADD = Menu.FIRST; private static final int MENU_DELETE = Menu.FIRST+1; private static final int MENU_UPDATE = Menu.FIRST+2; @Override 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_UPDATE, 0, "UPDATE"); return true; } @Override 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; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sqlite_activity); setUpViews(); } public void setUpViews(){ mBookDB = new BookDB(this); //进入这个界面 直接查询数据库 mCursor = mBookDB.select(); bookName = (EditText)findViewById(R.id.bookname); bookAuthor = (EditText)findViewById(R.id.bookauthor); bookList = (ListView)findViewById(R.id.booklist); // System.out.println(bookName); // System.out.println(bookAuthor); // System.out.println(bookList); BookListAdapter adapter = new BookListAdapter(SQLiteDatabaseDemo.this,mCursor); bookList.setAdapter(adapter); bookList.setOnItemClickListener(SQLiteDatabaseDemo.this); } //listview点击事件 @Override public void onItemClick(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 void add(){ //获取输入的内容 String bookname = bookName.getText().toString(); String author = bookAuthor.getText().toString(); if(bookname.equals("")||author.equals("")) return; mBookDB.add(bookname, author); //Requery 方法是通过重新查询窗体或控件的数据源更新基于指定窗体的数据 mCursor.requery(); //对listview自动刷新 bookList.invalidateViews(); bookName.setText(""); bookAuthor.setText(""); Toast.makeText(this, "add success", Toast.LENGTH_SHORT).show(); } public void delete(){ String bookname = bookName.getText().toString(); String author = bookAuthor.getText().toString(); if(BOOK_ID == 0) return; mBookDB.delete(BOOK_ID); mCursor.requery(); // //对listview自动刷新 bookList.invalidateViews(); bookName.setText(""); bookAuthor.setText(""); Toast.makeText(this, "delete success", Toast.LENGTH_SHORT).show(); } public void update(){ String bookname = bookName.getText().toString(); String author = bookAuthor.getText().toString(); if(bookname.equals("")||author.equals("")) return; mBookDB.update(BOOK_ID, bookname, author); mCursor.requery(); bookList.invalidateViews(); bookName.setText(""); bookAuthor.setText(""); Toast.makeText(this, "update success", Toast.LENGTH_SHORT).show(); } public class BookListAdapter extends BaseAdapter{ private Context mContext; private Cursor mCursor; public BookListAdapter(Context context,Cursor cursor){ mContext = context; mCursor = cursor; } @Override public int getCount() { //数据库数据条目 return mCursor.getCount(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { mCursor.moveToPosition(position); TextView mTextView = new TextView(mContext); mTextView.setText("序号"+mCursor.getString(1)+"内容"+mCursor.getString(2)); return mTextView; } } }
(1)数据库工具类BookDB
public class BookDB extends SQLiteOpenHelper{ //数据库名称 private final static String DATABASE_NAME = "BOOKS.db"; //数据库版本号 private final static int DATABASSE_VERSION=1; //表名 private final static String TABLE_NAME = "books_table"; //书籍名称 private final static String BOOK_NAME = "book_name"; ///书籍的id private final static String BOOK_ID = "book_id"; //作者 private final static String BOOK_AUTHOR = "book_author"; //构造方法 public BookDB(Context context) { //上下文 数据库名称 工厂类 数据库的版本号 super(context, DATABASE_NAME,null,DATABASSE_VERSION); } //第一次进来 如果没有 则创建数据库 如果有就打开 @Override public void onCreate(SQLiteDatabase db) { 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) { 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 add(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; } //根据id删除 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 bookauthor){ SQLiteDatabase db =this.getWritableDatabase(); ContentValues cv =new ContentValues(); cv.put(BOOK_NAME, bookname); cv.put(BOOK_AUTHOR, bookauthor); String where = BOOK_ID+"=?"; String[] whereValue = {Integer.toString(id)}; db.update(TABLE_NAME, cv, where, whereValue); } }