国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android SQLite事務處理結合Listview列表顯示功能示例

2019-12-12 02:24:01
字體:
來源:轉載
供稿:網友

本文實例講述了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添加到集合里

對于

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);

第四個參數里的"userid","username","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, null); //得到游標,類似resultset  //cursor.close(); //這里不可以關閉游標  return cursor;}

這里為主鍵的列取了別名是因為android內部建議主鍵設置為_id,但是不可能每個表的主鍵的名稱都是_id

Cursor all=this.service.fiandAllCursor(); //使用游標適配器SimpleCursorAdapter cadapter=new SimpleCursorAdapter(this, R.layout.listview,all, new String[]{"_id","username","address"},new int[]{R.id.userid,R.id.username,R.id.address});view.setAdapter(cadapter);//添加列表項監聽事件view.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView<?> parent, View view,int position, 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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平塘县| 威远县| 石阡县| 越西县| 龙泉市| 襄樊市| 即墨市| 荣昌县| 青海省| 绥阳县| 扎鲁特旗| 庆元县| 开化县| 肥西县| 巢湖市| 商都县| 炉霍县| 巧家县| 广德县| 乌什县| 唐河县| 晋城| 嘉祥县| 鄂托克前旗| 乐山市| 长丰县| 鄂尔多斯市| 鄂托克前旗| 永城市| 铜陵市| 遵义县| 阳信县| 湘潭县| 邵东县| 新民市| 平舆县| 大渡口区| 和林格尔县| 商丘市| 抚远县| 昭通市|