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

