1.定义DtatBase类:
package com.example.sqlitetest; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; public class Db extends sqliteOpenHelper { public Db(Context context) { super(context,"db",null,1); } @Override public void onCreate(sqliteDatabase arg0) { arg0.execsql("CREATE TABLE user(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT DEFAULT \"\"," + "age INTEGER DEFAULT \"\")"); } @Override public void onUpgrade(sqliteDatabase arg0,int arg1,int arg2) { // TODO Auto-generated method stub } }2.MainActivity中实现:
package com.example.sqlitetest; import android.app.AlertDialog; import android.app.ListActivity; import android.content.ContentValues; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.SimpleCursorAdapter; public class MainActivity extends ListActivity { private SimpleCursorAdapter adapter; private Button btnAdd; private EditText etName,etAge; private Db db; private sqliteDatabase dbRead,dbWrite; private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View arg0) { ContentValues cv = new ContentValues(); cv.put("name",etName.getText().toString()); cv.put("age",Integer.parseInt(etAge.getText().toString())); dbWrite.insert("user",cv); refersh(); } }; private void refersh() { Cursor c = dbRead.query("user",null); adapter.changeCursor(c); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName = (EditText) findViewById(R.id.etName); etAge = (EditText) findViewById(R.id.etAge); btnAdd = (Button) findViewById(R.id.btnAdd); btnAdd.setOnClickListener(clickListener); db = new Db(this); dbRead = db.getReadableDatabase(); dbWrite = db.getWritableDatabase(); adapter = new SimpleCursorAdapter(this,R.layout.db_view,new String[] { "name","age" },new int[] { R.id.tvName,R.id.tvAge }); setListAdapter(adapter); refersh(); getListView().setOnItemLongClickListener(ListViewLongClickListenner); } private OnItemLongClickListener ListViewLongClickListenner = new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0,View arg1,final int arg2,long arg3) { new AlertDialog.Builder(MainActivity.this) .setTitle("提醒") .setMessage("确定删除选中项吗") .setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0,int arg1) { Cursor c = adapter.getCursor(); c.moveToPosition(arg2); int itemId = c.getInt(c .getColumnIndex("_id")); dbWrite.delete("user","_id=?",new String[] { itemId + "" }); refersh(); } }).setNegativeButton("取消",null).show(); return true; } }; }3.布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > </ListView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NAME:" /> <EditText android:id="@+id/etName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AGE:" /> <EditText android:id="@+id/etAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ADD" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tvAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout>原文链接:https://www.f2er.com/sqlite/199268.html