Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android SQLite事务处理结合Listview列表显示功能示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了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的使用,我们理解成列表就可以了。界面如下

Android SQLite事务处理结合Listview列表显示功能示例

我们可以把这个界面拆成2个,主界面就只有“用户id”,“用户名”,“用户住址”也就是列表的头,主界面如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_49_11845@as.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<LinearLayout
 xmlns:android="http://scheR_49_11845@as.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://scheR_49_11845@as.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>();

这里的hashmap存储的是泛型数据,这个集合的泛型不能随便修改,接下来的工作就是把这个集合当做list的泛型

List<HashMap<String,Object>>();

最后要记得把这个map添加到集合里

对于

SimpleAdapter adapter=new SimpleAdapter(this,R.id.address});
    view.setAdapter(adapter);

第四个参数里的"userid","address"是map集合里的key,最后一个参数是textview,也就是数据界面里的textview.后面还加了个监听,只要点击textview就会显示用户id,android就会通过textview的位置读取内容

这里把先读数据的代码先贴出来

public List<student> fiandAll(){
  List<student> all=new ArrayList<student>();
  String sql="SELEct * from student";
  sqliteDatabase database=db.getReadableDatabase(); //使用getReadableDatabase取得sqliteDatabase
  cursor cursor=database.rawQuery(sql,null); //得到游标,类似resultset
  student stu;
  while(cursor.moveToNext()){ //移动游标
    int id=cursor.geTint(cursor.getcolumnIndex("userid"));
    String name=cursor.getString(cursor.getcolumnIndex("username"));
    String address=cursor.getString(cursor.getcolumnIndex("address"));
    stu=new student();
    stu.setUserid(id);
    stu.setUsername(Name);
    stu.setAddress(address);
    all.add(stu);
  }
  cursor.close(); //关闭游标
  return all;
}

(2)游标适配器

下面是读数据的代码

public cursor fiandAllcursor(){
  List<student> all=new ArrayList<student>();
  String sql="SELEct userid as _id,username,address from student";
  sqliteDatabase database=db.getReadableDatabase(); //使用getReadableDatabase取得sqliteDatabase
  cursor cursor=database.rawQuery(sql,类似resultset
  //cursor.close(); //这里不可以关闭游标
  return cursor;
}

这里为主键的列取了别名是因为android内部建议主键设置为_id,但是不可能每个表的主键的名称都是_id

cursor all=this.service.fiandAllcursor(); //使用游标适配器
SimplecursorAdapter cadapter=new SimplecursorAdapter(this,all,new String[]{"_id",R.id.address});
view.setAdapter(cadapter);
//添加列表项监听事件
view.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,long id) {
  ListView listview=(ListView)parent;
  cursor hash=(cursor)listview.getItemAtPosition(position); //取得被点击item的位置
  int temp=hash.geTint(hash.getcolumnIndex("_id"));
  Toast.makeText(DBACtivity.this,String.valueOf(temp),1).show();
}});

这里的适配器参数顺序和上面的有点不同,而且第四个参数里的“usernam”,"address"和'_id'都是表的列名。其他地方没太大区别,上面的“_id”也不能写成别的。否则会出错

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作SQLite数据库技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

大佬总结

以上是大佬教程为你收集整理的Android SQLite事务处理结合Listview列表显示功能示例全部内容,希望文章能够帮你解决Android SQLite事务处理结合Listview列表显示功能示例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: