这是我在网站看到过一个网友做的sqlite一个小程序,觉得写的不错,以供提示参考,所以把转载下来了。其实就是一个小收藏夹。
代码如下:
ActivityMain_1.java
public class ActivityMain_1 extends Activity { //声明EditText实例 private EditText et1,et2,et3; //声明Button实例 private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main_1); //根据ID获得实例对象 et1 = (EditText) findViewById(R.id.EditText01); et2 = (EditText) findViewById(R.id.EditText02); et3 = (EditText) findViewById(R.id.EditText03); button = (Button) findViewById(R.id.button); //事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=et1.getText().toString(); String url =et2.getText().toString(); String desc = et3.getText().toString(); ContentValues values = new ContentValues(); values.put("name",name); values.put("url",url); values.put("desc",desc); //实例化数据库帮助类 DBHelper helper = new DBHelper(getApplicationContext()); //打开数据库 helper.open(); //插入数据 helper.insert(values); //实例化Intent Intent intent = new Intent(ActivityMain_1.this,QueryActivity.class); startActivity(intent); helper.close(); } }); } }
QueryActivity.java
public class QueryActivity extends ListActivity { private ListView listView; private Cursor c; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //setContentView(R.layout.row); //实例化数据库帮助类 final DBHelper help = new DBHelper(this); help.open(); c= help.query(); query(); //提示对话框 final AlertDialog.Builder builder = new AlertDialog.Builder(this); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent,View view,int position,long id) { final long temp = id; builder.setMessage("真的要删除该条记录吗?").setPositiveButton("是",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { System.out.println("yes"); help.del((int)temp); //重新查询 //query(); Cursor c = help.query(); //列表项数组 String[] from ={"_id","name","url","desc"}; //列表项ID int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3}; //适配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row,c,from,to); ListView listView = getListView(); listView.setAdapter(adapter); } }).setNegativeButton("否",int which) { // TODO Auto-generated method stub } }); builder.create(); builder.show(); } }); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); } public void query(){ //列表项数组 String[] from ={"_id","desc"}; //列表项ID int[] to ={R.id.text0,R.id.text3}; //适配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,to); //adapter.notifyDataSetChanged(); //列表视图 listView = getListView(); //为列表视图添加适配器 listView.setAdapter(adapter); } }
DBHelper.java
public class DBHelper extends sqliteOpenHelper { private static final String DB_NAME="coll.db"; //表名 private static final String TBL_NAME="logtable"; //创建sql语句 private static final String CREATE_TBL = "create table logtable " + "(_id integer primary key autoincrement,name text,url text,desc text)"; //sqliteDatabase实例 private sqliteDatabase db; public DBHelper(Context context) { super(context,DB_NAME,null,2); } @Override public void onCreate(sqliteDatabase db) { db.execsql(CREATE_TBL); } //打开数据库 public void open(){ db = getWritableDatabase(); } @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // TODO Auto-generated method stub } //插入方法 public void insert(ContentValues values){ db.insert(TBL_NAME,values); } //查询方法 public Cursor query(){ Cursor c = db.query(TBL_NAME,null); return c; } //删除方法 public void del(int id){ db.delete(TBL_NAME,"_id=?",new String[]{String.valueOf(id)}); } //关闭数据库 public void close(){ if (db != null){ db.close(); } } }
main_1.xml
<?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" > <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="网站名称" /> <EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="URL" /> <EditText android:id="@+id/EditText02" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="网站描述" /> <EditText android:id="@+id/EditText03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:height="100px" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </LinearLayout>
自定义布局:row.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" > <TextView android:id="@+id/text0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px" /> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px" /> </LinearLayout>
结果如图所示: