SQLite(二)适配器,数据库事务
(1)适配器
SQLite适配器 SimpleCursorAdapter
(注意!使用SimpleCursorAdapter适配器主键的名字一点要是_id,不然会报错)
SimpleCursorAdapter作用是把cursor游标中保存的数据放到ListView控件中
使用ListView的时候,因为ListView里面有多个子布局,所以需要定义子布局。
子布局的代码:
1 26 7 15 16 24 25 33 34
from和to的意思是把数据库中的Constant.ID放到text1控件中,把Constant.AGE放到text2中,把Constant.NAME放到text3中,重复直到把cursor中的数据读取完,
有两种使用方法:
一:
1 public void Show(SQLiteDatabase DB){ 2 cursor = operation.Select(null, null, null); 3 /*SimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags); 4 * Context context : 上下文对象 5 * int layout : ListView里面有多个子布局,这个是子布局的id 6 * Cursor cursor : 数据源 7 * String[] from : 表示cursor中数据表字段的数组 8 * int[] to : 表示将对应的字段的数据放到子布局对应的子控件中,数据里面是子控件的id 9 * int flags : 设置适配器的标志10 * 注意使用SimpleCursorAdapter主键名字必须是_id11 */12 SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.litem, cursor, 13 new String[]{Constant.ID,Constant.AGE,Constant.NAME}, new int[]{R.id.text1,R.id.text2,R.id.text3}, 14 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);15 lv.setAdapter(adapter);16 DB.close();17 }
二:
1 public void Show2(SQLiteDatabase DB){2 cursor = operation.Select(null, null, null);3 MyCursorAdapter adapter = new MyCursorAdapter(MainActivity.this,cursor,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);4 lv.setAdapter(adapter);5 DB.close();6 }
1 public class MyCursorAdapter extends CursorAdapter { 2 3 public MyCursorAdapter(Context context, Cursor cursor, int flags) { 4 super(context, cursor, flags); 5 } 6 @Override 7 /* 8 *ViewGroup : listView布局的id 9 *返回listView的子控件10 */11 public View newView(Context context, Cursor cursor, ViewGroup arg2) {12 View view = LayoutInflater.from(context).inflate(R.layout.litem, null);13 return view;14 }15 @Override16 /*17 * view 是newView返回的18 */19 public void bindView(View view, Context context, Cursor cursor) {20 TextView id = (TextView) view.findViewById(R.id.text1);21 TextView age = (TextView) view.findViewById(R.id.text2);22 TextView name = (TextView) view.findViewById(R.id.text3);23 id.setText(cursor.getInt(cursor.getColumnIndex(Constant.ID))+"");24 age.setText(cursor.getInt(cursor.getColumnIndex(Constant.AGE))+"");25 name.setText(cursor.getString(cursor.getColumnIndex(Constant.NAME))+"");26 }27 }
两种方法效果都一样,这个两个是上一篇的Show()和Show2()方法。
(2)数据库事务
数据库事务Transaction指的是在数据库批量操作的时候使用的。
按照上诉方法,如果要插入100条数据,每一次都要打开关闭数据库,影响效率,如果使用了数据库事务,可以实现打开一次进行批量插入。批量删除更新也是,一单批量操作的任何一条操作有错误,都会出错不进行。
实现数据库事务一共需要三步,开始事务,提交事务,关闭事务。
示例代码:
1 public class MainActivity extends Activity { 2 3 private MySQLiteOpenHelper helper = null; 4 private ListView lv = null; 5 SimpleCursorAdapter adapter = null; 6 int n =1; 7 @SuppressLint("NewApi") @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState);10 setContentView(R.layout.activity_main);11 lv = (ListView)findViewById(R.id.lv);12 if(helper == null){13 helper = new MySQLiteOpenHelper(this);14 }15 SQLiteDatabase DB = helper.getWritableDatabase(); 16 //1.数据库显示开启事务17 DB.beginTransaction();18 while(n<10){19 ContentValues values = new ContentValues();20 values.put("_id", n);21 values.put("age", 1);22 values.put("name", "wlj");23 DB.insert("A", null, values);24 n++;25 }26 //2.提交当前事务27 DB.setTransactionSuccessful();28 //3.关闭事务29 DB.endTransaction();30 Cursor cursor = DB.query("a", null, null, null, null, null, null);31 SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list, cursor, 32 new String[]{"_id","age","name"}, new int[]{R.id.text1,R.id.text2,R.id.text3}, 33 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);34 lv.setAdapter(adapter);35 DB.close();36 }37 }
补充,要获得Cursor中的资源的方法:
1 while(cursor.moveToNext()){2 String id = cursor.getString(cursor.getColumnIndex("_id"));3 String age = cursor.getString(cursor.getColumnIndex("age"));4 String name = cursor.getString(cursor.getColumnIndex("name"));5 Log.i("tag",id+" "+age+" "+name);6 }
如果有什么错误,或者我理解错误或不当的,恳请大家纠正,谢谢!嘻嘻嘻~