本文整理自网络,侵删。
本文实例讲述了Android SQLite事务处理结合Listview列表显示功能。分享给大家供大家参考,具体如下:
前面的文章里介绍过事务的特点如原子性,隔离性,一致性,持久性。下面就结合Android的sqlite来说下,这次的文章里会把listview也结合起来用。实际上android里的事务和我们数据库里的是一样的。也是开启事务,操作,提交事务。如果出现问题就回滚。
public void Transaction(){ SQLiteDatabase database=db.getReadableDatabase(); database.beginTransaction(); //开启事务 try{ String sql1="update student set username='lili' where userid=2"; String sql2="update student set username='lucy' where userid=3"; database.execSQL(sql1); database.execSQL(sql2); database.setTransactionSuccessful(); //设置事务的状态,这句不写事务就会回滚 }finally{ database.endTransaction(); //结束事务 } }
上面这段代码就是一个简单的事务操作,需要注意的就是要捕获异常,这样事务就会被结束掉可以节约数据库资源。
事务的操作就是这样,下面就介绍下listview的使用,我们理解成列表就可以了。界面如下
我们可以把这个界面拆成2个,主界面就只有“用户id”,“用户名”,“用户住址”也就是列表的头,主界面如下
<?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" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="60dip" android:layout_height="wrap_content" android:text="用户id" /> <TextView android:layout_width="60dip" android:layout_height="wrap_content" android:text="用户名" /> <TextView android:layout_width="60dip" android:layout_height="wrap_content" android:text="用户住址" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listview" /> </LinearLayout>
这里的listview要定义一个id提供后面数据绑定使用,含有内容的显示界面也比较简单,也就是几个textview
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="60dip" android:layout_height="wrap_content" android:id="@+id/userid" /> <TextView android:layout_width="60dip" android:layout_height="wrap_content" android:id="@+id/username" /> <TextView android:layout_width="90dip" android:layout_height="wrap_content" android:id="@+id/address" /> </LinearLayout>
这样界面的部分就OK了,接下来就是读取数据了,之后显示在listview中,在这里就提供2种方法来显示数据
(1)方法1
package org.lxh.db; import java.util.*; import org.lxh.service.StudentService; import org.lxh.vo.Student; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class DBActivity extends Activity { private StudentService service; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.service=new StudentService(this); ListView view=(ListView)this.findViewById(R.id.listview); List<Student> all=this.service.fiandAll(); List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>(); //逐个取出元素 Iterator<Student> it=all.iterator(); Student stu=null; while(it.hasNext()){ stu=new Student(); stu=it.next(); HashMap<String,Object> map=new HashMap<String,Object>(); map.put("userid", stu.getUserid()); map.put("username", stu.getUsername()); map.put("address", stu.getAddress()); data.add(map); } //数据绑定 SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.listview, new String[]{"userid","username","address"},new int[]{R.id.userid,R.id.username,R.id.address}); view.setAdapter(adapter); //添加列表项监听事件 view.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> parent, View view,int position, long id) { ListView listview=(ListView)parent; HashMap<String,Object> hash=(HashMap<String,Object>)listview.getItemAtPosition(position); Toast.makeText(DBActivity.this, hash.get("userid").toString(), 1).show(); }}); }
这里的数据绑定,使用的是SimpleAdapter,我们首先要做的就是把数据逐个取出来存入一个HashMap,如下所示
HashMap<String,Object> map=new HashMap<String,Object>();
这里的hashmap存储的是泛型数据,这个集合的泛型不能随便修改,接下来的工作就是把这个集合当做list的泛型
List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
最后要记得把这个map添加到集合里
相关阅读 >>
unity3d运行报dllnotfoundexception错误的解决方案
更多相关阅读请进入《Sqlite》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。