Android SQLite事務(wù)處理結(jié)合Listview列表顯示功能示例
本文實(shí)例講述了Android SQLite事務(wù)處理結(jié)合Listview列表顯示功能。分享給大家供大家參考,具體如下:
前面的文章里介紹過事務(wù)的特點(diǎn)如原子性,隔離性,一致性,持久性。下面就結(jié)合Android的sqlite來說下,這次的文章里會(huì)把listview也結(jié)合起來用。實(shí)際上android里的事務(wù)和我們數(shù)據(jù)庫里的是一樣的。也是開啟事務(wù),操作,提交事務(wù)。如果出現(xiàn)問題就回滾。
public void Transaction(){ SQLiteDatabase database=db.getReadableDatabase(); database.beginTransaction(); //開啟事務(wù) 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(); //設(shè)置事務(wù)的狀態(tài),這句不寫事務(wù)就會(huì)回滾 }finally{ database.endTransaction(); //結(jié)束事務(wù) } }
上面這段代碼就是一個(gè)簡(jiǎn)單的事務(wù)操作,需要注意的就是要捕獲異常,這樣事務(wù)就會(huì)被結(jié)束掉可以節(jié)約數(shù)據(jù)庫資源。
事務(wù)的操作就是這樣,下面就介紹下listview的使用,我們理解成列表就可以了。界面如下
我們可以把這個(gè)界面拆成2個(gè),主界面就只有“用戶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要定義一個(gè)id提供后面數(shù)據(jù)綁定使用,含有內(nèi)容的顯示界面也比較簡(jiǎn)單,也就是幾個(gè)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了,接下來就是讀取數(shù)據(jù)了,之后顯示在listview中,在這里就提供2種方法來顯示數(shù)據(jù)
(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>>(); //逐個(gè)取出元素 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); } //數(shù)據(jù)綁定 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); //添加列表項(xiàng)監(jiān)聽事件 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(); }}); }
這里的數(shù)據(jù)綁定,使用的是SimpleAdapter,我們首先要做的就是把數(shù)據(jù)逐個(gè)取出來存入一個(gè)HashMap,如下所示
HashMap<String,Object> map=new HashMap<String,Object>();
這里的hashmap存儲(chǔ)的是泛型數(shù)據(jù),這個(gè)集合的泛型不能隨便修改,接下來的工作就是把這個(gè)集合當(dāng)做list的泛型
List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
最后要記得把這個(gè)map添加到集合里
對(duì)于
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);
第四個(gè)參數(shù)里的"userid","username","address"是map集合里的key,最后一個(gè)參數(shù)是textview,也就是數(shù)據(jù)界面里的textview.后面還加了個(gè)監(jiān)聽,只要點(diǎn)擊textview就會(huì)顯示用戶id,android就會(huì)通過textview的位置讀取內(nèi)容。
這里把先讀數(shù)據(jù)的代碼先貼出來
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); //得到游標(biāo),類似resultset Student stu; while(cursor.moveToNext()){ //移動(dòng)游標(biāo) 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(); //關(guān)閉游標(biāo) return all; }
(2)游標(biāo)適配器
下面是讀數(shù)據(jù)的代碼
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); //得到游標(biāo),類似resultset //cursor.close(); //這里不可以關(guān)閉游標(biāo) return cursor; }
這里為主鍵的列取了別名是因?yàn)閍ndroid內(nèi)部建議主鍵設(shè)置為_id,但是不可能每個(gè)表的主鍵的名稱都是_id
Cursor all=this.service.fiandAllCursor(); //使用游標(biāo)適配器 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); //添加列表項(xiàng)監(jiān)聽事件 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); //取得被點(diǎn)擊item的位置 int temp=hash.getInt(hash.getColumnIndex("_id")); Toast.makeText(DBActivity.this, String.valueOf(temp), 1).show(); }});
這里的適配器參數(shù)順序和上面的有點(diǎn)不同,而且第四個(gè)參數(shù)里的“usernam”,"address"和'_id'都是表的列名。其他地方?jīng)]太大區(qū)別,上面的“_id”也不能寫成別的。否則會(huì)出錯(cuò)
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程之SQLite數(shù)據(jù)庫操作方法詳解
- Android開發(fā)筆記SQLite優(yōu)化記住密碼功能
- Android利用listview控件操作SQLite數(shù)據(jù)庫實(shí)例
- Android批量插入數(shù)據(jù)到SQLite數(shù)據(jù)庫的方法
- Android SQLite數(shù)據(jù)庫基本操作方法
- Android使用SQLite數(shù)據(jù)庫的示例
- android SQLite數(shù)據(jù)庫總結(jié)
- Android SQLite數(shù)據(jù)庫版本升級(jí)的管理實(shí)現(xiàn)
相關(guān)文章
Android自動(dòng)獲取輸入短信驗(yàn)證碼庫AutoVerifyCode詳解
這篇文章主要為大家詳細(xì)介紹了Android自動(dòng)獲取輸入短信驗(yàn)證碼庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07基于Android studio3.6的JNI教程之helloworld思路詳解
這篇文章主要介紹了基于Android studio3.6的JNI教程之helloworld,本文通過圖文實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android 利用ViewPager+GridView實(shí)現(xiàn)首頁導(dǎo)航欄布局分頁效果
用ViewPager+GridView實(shí)現(xiàn)首頁導(dǎo)航欄布局分頁效果來實(shí)現(xiàn)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android UI:ListView - SimpleAdapter實(shí)例詳解
這篇文章主要介紹了Android UI:ListView - SimpleAdapter實(shí)例詳解,SimpleAdapter是擴(kuò)展性最好的適配器,可以定義各種你想要的布局,而且使用很方便,需要的朋友可以參考下2016-11-11Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例
這篇文章主要介紹了Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Flutter實(shí)現(xiàn)一個(gè)支持漸變背景的Button示例詳解
這篇文章主要為大家介紹了Flutter實(shí)現(xiàn)一個(gè)支持漸變背景的Button示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊
這篇文章主要為大家介紹了Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android IPC機(jī)制利用Messenger實(shí)現(xiàn)跨進(jìn)程通信
這篇文章主要介紹了Android IPC機(jī)制中 Messager 實(shí)現(xiàn)跨進(jìn)程通信的知識(shí),對(duì)Android學(xué)習(xí)通信知識(shí)非常重要,需要的同學(xué)可以參考下2016-07-07