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(); //结束事务
}
}
<?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>
<?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>
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();
}});
}
HashMap<String,Object> map=new HashMap<String,Object>();
List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
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);
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;
}
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;
}
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();
}});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有