
昨天简单地做一了一个列表展示,现在将增加/修改/删除方法都给加上了,本次增加的内容比较多,比如常见按钮的事件,列表长按事件,Activity之间的跳转及传递参数,按键事件的监听等,希望对初学的朋友有些帮助,同时欢迎老鸟给我做Code revIEw,需要改进的地方,还请指出,谢谢。
下面逐个介绍一个代码,数据库辅助类:DBOpenHelper
package com.van.sqlite.db;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;/** * 数据库辅助类。 * @author Van * */public class DBOpenHelper extends sqliteOpenHelper{ /**数据库名称*/ private static final String DATABASE_name="MyAppDB"; /**数据库版本*/ private static final int DATABASE_VERSION=1; /** 创建数据表语句*/ private static final String DDL_CREATE_table_APPINFO="CREATE table IF NOT EXISTS AppInfo (appID integer primary key autoincrement,appname text,appDescription text,remark text)"; /** * 实例化数据库连接. * @param context */ public DBOpenHelper(Context context){ super(context,DATABASE_name,null,DATABASE_VERSION); /**初始化数据表*/ this.getWritableDatabase().execsql(DDL_CREATE_table_APPINFO); } @OverrIDe public voID onCreate(sqliteDatabase db) { // Todo auto-generated method stub } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // Todo auto-generated method stub }} 针对AppInfo数据表的辅助类,内含增删查改方法:AppInfoHelper
package com.van.sqlite.db;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;public class AppInfoHelper { private DBOpenHelper helper; /** * 构造 * @param context */ public AppInfoHelper(Context context){ this.helper=new DBOpenHelper(context); } /** * 查询 * @param tablename * @return */ public Cursor select(String sql) { sqliteDatabase db=helper.getWritableDatabase(); try{ Cursor cursor=db.rawquery(sql,null); cursor.movetoFirst(); return cursor; }catch(Exception ex){ ex.printstacktrace(); }finally{ if(db.isopen()){ db.close(); } } return null; } /** * 新增方法. * @param appname * @param appDescription * @param remark */ public voID addAppInfo(String appname,String appDescription,String remark){ ContentValues values=new ContentValues(); values.put("appname",appname); values.put("appDescription",appDescription); values.put("remark",remark); sqliteDatabase db=helper.getWritableDatabase(); try{ db.insert("AppInfo",values); }catch(Exception ex){ ex.printstacktrace(); }finally{ if(db.isopen()){ db.close(); } } } /** * 编辑应用信息. * @param appID * @param appname * @param appDescription * @param remark */ public voID editAppInfo(int appID,String appname,String remark){ ContentValues values=new ContentValues(); values.put("appname",remark); sqliteDatabase db=helper.getWritableDatabase(); //条件 String[] whereValue ={ Integer.toString(appID) }; try{ db.update("AppInfo",values,"appID=?",whereValue); }catch(Exception ex){ ex.printstacktrace(); }finally{ if(db.isopen()){ db.close(); } } } /** * 删除应用信息. * @param appID */ public voID deleteAppInfo(int appID){ sqliteDatabase db=helper.getWritableDatabase(); //条件 String[] whereValue ={ Integer.toString(appID) }; try{ db.delete("AppInfo",whereValue); }catch(Exception ex){ ex.printstacktrace(); }finally{ if(db.isopen()){ db.close(); } } } }
AppInfo数据适配器,用于列表显示的适配器:AppInfolistadapter
package com.van.sqlite.adapter;import java.util.ArrayList;import java.util.HashMap;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.Widget.SimpleAdapter;import com.van.sqlite.activity.R;import com.van.sqlite.db.DBOpenHelper;public class AppInfolistadapter { private Context context; public AppInfolistadapter(Context context){ this.context=context; } /** * 查询AppInfo返回Map集合 * @return */ private ArrayList<HashMap<String,Object>> fillList(){ //生成动态数组,并且转载数据 ArrayList<HashMap<String,Object>> dataList = new ArrayList<HashMap<String,Object>>(); DBOpenHelper helper=new DBOpenHelper(context); sqliteDatabase db=helper.getReadableDatabase(); try{ Cursor cursor=db.rawquery("SELECT * FROM AppInfo",null); cursor.movetoFirst(); if(cursor.movetoFirst()) { while (!cursor.isAfterLast()) { Integer appID = cursor.getInt(cursor.getColumnIndex("appID")); String appname = cursor.getString(cursor.getColumnIndex("appname")); String appDescription = cursor.getString(cursor.getColumnIndex("appDescription")); HashMap<String,Object> map = new HashMap<String,Object>(); map.put("appID",appID); map.put("appname",appname); map.put("appDescription",appDescription); dataList.add(map); cursor.movetoNext(); } } }catch(Exception ex){ ex.printstacktrace(); }finally{ if(db.isopen()){ db.close(); } } return dataList; } /** * 填充数据,取得数据适配器. * @param ListData * @return */ public SimpleAdapter getAdapter(Context context){ //生成适配器,数组===》ListItem SimpleAdapter adapter = new SimpleAdapter(context,fillList(),//数据来源 R.layout.List_item,//ListItem的XML实现 //动态数组与ListItem对应的子项 new String[] {"appname","appDescription"},//ListItem的XML文件里面的两个TextVIEw ID new int[] {R.ID.textVIEw_appname,R.ID.textVIEw_appDescription}); return adapter; } }
3个Activity,主界面显示/增加/修改
sqliteDemoActivity
package com.van.sqlite.activity; import java.util.HashMap;import androID.app.Activity;import androID.app.AlertDialog;import androID.app.AlertDialog.Builder;import androID.content.DialogInterface;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.ContextMenu;import androID.vIEw.ContextMenu.ContextMenuInfo;import androID.vIEw.KeyEvent;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEw.OnCreateContextMenuListener;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.Toast;import com.van.sqlite.adapter.AppInfolistadapter;import com.van.sqlite.db.AppInfoHelper; public class sqliteDemoActivity extends Activity { private ListVIEw ListVIEw; private AppInfolistadapter adapter; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); ListVIEw=(ListVIEw)findVIEwByID(R.ID.ListVIEw_appList); //获取数据适配器 adapter=new AppInfolistadapter(this); //添加并且显示 ListVIEw.setAdapter(adapter.getAdapter(this)); //跳转到新增窗口 button addbutton=(button)findVIEwByID(R.ID.button_to_add); addbutton.setonClickListener(new OnClickListener(){ @OverrIDe public voID onClick(VIEw arg0) { //打开新的窗口 Intent intent=new Intent(); intent.setClass(sqliteDemoActivity.this,AddAppInfoActivity.class); startActivity(intent); //结束当前 sqliteDemoActivity.this.finish(); } }); //数据列表点击 *** 作 ListVIEw.setonItemClickListener(new OnItemClickListener() { @SuppressWarnings("unchecked") @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw,VIEw vIEw,int index,long arg3) { ListVIEw ListVIEw = (ListVIEw)adapterVIEw; HashMap<String,Object> map = (HashMap<String,Object>) ListVIEw.getItemAtposition(index); //打开新的窗口 Intent intent=new Intent(); intent.setClass(sqliteDemoActivity.this,EditAppInfoActivity.class); //将appID传入编辑窗口 intent.putExtra("appID",Integer.parseInt(map.get("appID").toString())); startActivity(intent); //结束当前 sqliteDemoActivity.this.finish(); } }); //添加长按点击 ListVIEw.setonCreateContextMenuListener(new OnCreateContextMenuListener() { @OverrIDe public voID onCreateContextMenu(ContextMenu menu,VIEw v,ContextMenuInfo menuInfo) { menu.setheaderTitle("列表 *** 作"); menu.add(0,"删除应用信息"); } }); } @OverrIDe public boolean onKeyDown(int keyCode,KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){//返回按钮 AlertDialog.Builder builder = new Builder(sqliteDemoActivity.this); builder.setMessage("确实要退出程序吗?"); builder.setTitle("提示"); builder.setPositivebutton("确认",new androID.content.DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog,int which) { dialog.dismiss(); sqliteDemoActivity.this.finish(); } }); builder.setNegativebutton("取消",new androID.content.DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog,int which) { dialog.dismiss(); } }); builder.create().show(); return true; } return false; } /** * 删除应用. * @param appID */ public voID delete(int appID){ AppInfoHelper appInfoHelper=new AppInfoHelper(this); appInfoHelper.deleteAppInfo(appID); Toast.makeText(sqliteDemoActivity.this,"应用删除成功!",Toast.LENGTH_SHORT).show(); //添加并且显示 ListVIEw.setAdapter(adapter.getAdapter(this)); } //长按菜单响应函数 @SuppressWarnings("unchecked") @OverrIDe public boolean onContextItemSelected(MenuItem item) { AdapterVIEw.AdapterContextMenuInfo m = (AdapterVIEw.AdapterContextMenuInfo) item.getMenuInfo(); HashMap<String,Object>) ListVIEw.getItemAtposition(m.position); int appID=Integer.parseInt(map.get("appID").toString()); //调用方法,删除 delete(appID); return super.onContextItemSelected(item); } }
AddAppInfoActivity
package com.van.sqlite.activity;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.KeyEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import com.van.sqlite.db.AppInfoHelper;public class AddAppInfoActivity extends Activity{ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.add); button savebutton=(button)findVIEwByID(R.ID.button_save); savebutton.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { add();//添加数据 } }); } /** * 添加数据方法. */ public voID add(){ EditText et_appname=(EditText)findVIEwByID(R.ID.editText_appname); EditText et_appDescription=(EditText)findVIEwByID(R.ID.editText_appDescription); EditText et_remark=(EditText)findVIEwByID(R.ID.editText_remark); String appname=et_appname.getText().toString(); String appDescription=et_appDescription.getText().toString(); String remark=et_remark.getText().toString(); AppInfoHelper appInfoHelper=new AppInfoHelper(this); appInfoHelper.addAppInfo(appname,appDescription,remark); Toast.makeText(AddAppInfoActivity.this,"应用信息添加成功!",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(AddAppInfoActivity.this,sqliteDemoActivity.class); startActivity(intent); AddAppInfoActivity.this.finish(); } @OverrIDe public boolean onKeyDown(int keyCode,KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){//返回按钮 Intent intent = new Intent(); intent.setClass(AddAppInfoActivity.this,sqliteDemoActivity.class); startActivity(intent);// 启动新的Activity AddAppInfoActivity.this.finish();// 结束就的Activity } return false; } } EditAppInfoActivity
package com.van.sqlite.activity;import androID.app.Activity;import androID.content.Intent;import androID.database.Cursor;import androID.os.Bundle;import androID.vIEw.KeyEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import com.van.sqlite.db.AppInfoHelper;public class EditAppInfoActivity extends Activity{ private int paramAppID; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.edit); //绑定数据 bindValue(); //绑定事件 button editbutton=(button)findVIEwByID(R.ID.button_edit_save); editbutton.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { edit(); } }); } /** * 查询赋值. */ public voID bindValue(){ //取得编号 paramAppID= this.getIntent().getExtras().getInt("appID"); //查询数据 AppInfoHelper appInfoHelper=new AppInfoHelper(this); Cursor cursor=appInfoHelper.select("SELECT * FROM AppInfo WHERE appID="+paramAppID); if(cursor.movetoFirst()) { String appname = cursor.getString(cursor.getColumnIndex("appname")); String appDescription = cursor.getString(cursor.getColumnIndex("appDescription")); String remark = cursor.getString(cursor.getColumnIndex("remark")); EditText et_appname=(EditText)findVIEwByID(R.ID.editText_edit_appname); EditText et_appDescription=(EditText)findVIEwByID(R.ID.editText_edit_appDescription); EditText et_remark=(EditText)findVIEwByID(R.ID.editText_edit_remark); et_appname.setText(appname); et_appDescription.setText(appDescription); et_remark.setText(remark); } } /** * 编辑应用。 */ private voID edit(){ EditText et_appname=(EditText)findVIEwByID(R.ID.editText_edit_appname); EditText et_appDescription=(EditText)findVIEwByID(R.ID.editText_edit_appDescription); EditText et_remark=(EditText)findVIEwByID(R.ID.editText_edit_remark); String appname=et_appname.getText().toString(); String appDescription=et_appDescription.getText().toString(); String remark=et_remark.getText().toString(); AppInfoHelper appInfoHelper=new AppInfoHelper(this); appInfoHelper.editAppInfo(paramAppID,appname,remark); Toast.makeText(EditAppInfoActivity.this,"应用信息编辑成功!",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(EditAppInfoActivity.this,sqliteDemoActivity.class); startActivity(intent); EditAppInfoActivity.this.finish(); } @OverrIDe public boolean onKeyDown(int keyCode,KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){//返回按钮 Intent intent = new Intent(); intent.setClass(EditAppInfoActivity.this,sqliteDemoActivity.class); startActivity(intent);// 启动新的Activity EditAppInfoActivity.this.finish();// 结束就的Activity } return false; } }
几个布局文件:
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" > <button androID:ID="@+ID/button_to_add" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/add" /> <ListVIEw androID:ID="@+ID/ListVIEw_appList" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > </ListVIEw> </linearLayout>
List_item.xml
<?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/textVIEw_appname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <TextVIEw androID:ID="@+ID/textVIEw_appDescription" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceSmall" /></linearLayout>
add.xml
<?xml version="1.0" enCoding="utf-8"?><tableLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <TextVIEw androID:ID="@+ID/textVIEw_appname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/appname" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_appname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="textPostalAddress" /> <TextVIEw androID:ID="@+ID/textVIEw_appDescription" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/appDescription" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_appDescription" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="text" /> <TextVIEw androID:ID="@+ID/textVIEw_remark" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/remark" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_remark" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="text"/> <button androID:ID="@+ID/button_save" androID:layout_wIDth="100dp" androID:layout_margintop="15dp" androID:layout_height="wrap_content" androID:text="@string/save" /></tableLayout>
edit.xml
<?xml version="1.0" enCoding="utf-8"?><tableLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <TextVIEw androID:ID="@+ID/textVIEw_edit_appname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/appname" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_edit_appname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="textPostalAddress" /> <TextVIEw androID:ID="@+ID/textVIEw_edit_appDescription" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/appDescription" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_edit_appDescription" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="text" /> <TextVIEw androID:ID="@+ID/textVIEw_edit_remark" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/remark" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/editText_edit_remark" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:@R_502_5983@Type="text"/> <button androID:ID="@+ID/button_edit_save" androID:layout_wIDth="100dp" androID:layout_margintop="15dp" androID:layout_height="wrap_content" androID:text="@string/edit" /></tableLayout>
神马String变量就不贴了,看看效果先,主界面:
添加,点击主界面添加按钮进入添加页面:
编辑,点击列表直接跳转到编辑页面:
删除,长按类别项出现删除 *** 作:
总结
以上是内存溢出为你收集整理的SQLite进阶:Android上的SQLite常用 *** 作全部内容,希望文章能够帮你解决SQLite进阶:Android上的SQLite常用 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)