与sqllite的简单操作
ToDoDB.java
package src.com;
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 ToDoDB extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "todo_db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "todo_table";
public final static String FIELD_id = "_id";
public final static String FIELD_TEXT = "todo_text";
public ToDoDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id + " INTEGER primary key autoincrement, " +
" " + FIELD_TEXT + " 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 insert(String text){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
long row = db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id){
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue = {Integer.toString(id)};
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id, String text){
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue = {Integer.toString(id)};
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
DBActivity.java
package src.com;
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;
/** Called when the activity is first created. */
@Override
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);
/*取得DataBase里的数据*/
myCursor = myToDoDB.select();
/* new SimpleCursorAdapter并将myCursor传入,显示数据的字段为todo_text */
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, myCursor,
new String[] {ToDoDB.FIELD_TEXT}, new int[] {R.id.listTextView1});
myListView.setAdapter(adapter);
/* 将myListView添加OnItemClickListener */
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
/* 将myCursor移动到所点击的值 */
myCursor.moveToPosition(arg2);
/* 取得字段_id的值 */
_id = myCursor.getInt(0);
/* 取得字段todo_text的值 */
myEditText.setText(myCursor.getShort(1));
}
});
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
/* getSelectedItem所取得的是SQLiteCursor */
SQLiteCursor sc = (SQLiteCursor) parent.getSelectedItem();
_id = sc.getInt(0);
myEditText.setText(sc.getString(1));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
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;
}
private void addTodo(){
if(myEditText.getText().toString().equals("")){
return;
}
/* 添加数据到数据库 */
myToDoDB.insert(myEditText.getText().toString());
/* 重新查询 */
myCursor.requery();
/* 重新整理myListView */
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
private void editTodo() {
if(myEditText.getText().toString().equals("")){
return;
}
/* 修改数据 */
myToDoDB.update(_id, myEditText.getText().toString());
/* 重新查询 */
myCursor.requery();
/* 重新整理myListView */
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
private void deleteTodo() {
if(_id == 0){
return;
}
/* 删除数据 */
myToDoDB.delete(_id);
/* 重新查询 */
myCursor.requery();
/* 重新整理myListView */
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
}
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"
android:background="@drawable/white" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/black" />
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/black" />
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/red" >
</ListView>
</LinearLayout>
list.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" >
<CheckedTextView
android:id="@+id/listTextView1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/black" >
</CheckedTextView>"
</LinearLayout>