当前位置:数据库 > SQLite >>

Android心得4.5--SQLite数据库--事务处理、ListView列表显示

1.   使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,如果程序执行到endTransaction()之前调用了setTransactionSuccessful() 方法设置事务的标志为成功则提交事务,如果没有调用setTransactionSuccessful() 方法则回滚事务。使用例子如下:
 SQLiteDatabase db = ....;

db.beginTransaction();//开始事务

try {

    db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"传智播客", 4});

    db.execSQL("update person set name=? where personid=?", new Object[]{"传智", 1});

    db.setTransactionSuccessful();//调用此方易做图在执行到endTransaction() 时提交当前事务,如果不调用此方易做图回滚事务

} finally {

    db.endTransaction();//由事务的标志决定是提交事务,还是回滚事务

}

db.close();

上面两条SQL语句在同一个事务中执行。

2.ListView列表显示

   先在Layout文件夹下创建一个item.xml文件,水平线性布局显示文件,然后在main.xml文件下引用<ListView/>.

   网格显示跟列表显示基本上一样。

   有三种方法可以完成操作,分别是下面代码的show()、show2()、show3(),show3()是自定义的适配器,某些特殊情况下会用到。

代码如下:

package cn.itcast.db;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

 

import cn.itcast.adapter.PersonAdapter;

import cn.itcast.domain.Person;

import cn.itcast.service.PersonService;

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 MainActivity extends Activity {

    private ListView listView;

    private PersonService personService;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        personService = new PersonService(this);

      

        listView = (ListView) this.findViewById(R.id.listView);

        listView.setOnItemClickListener(new ItemClickListener());

        show2();

    }

  

    private final class ItemClickListener implements OnItemClickListener{

              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                     ListView lView = (ListView)parent;

                     /* 自定义适配器

                     Person person = (Person) lView.getItemAtPosition(position);

                     Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();*/

                   

                     Cursor cursor = (Cursor) lView.getItemAtPosition(position);

                     int personid = cursor.getInt(cursor.getColumnIndex("_id"));

                     Toast.makeText(getApplicationContext(), personid+ "", 1).show();

              }

    }

  

  

    //自定义适配器

       private void show3() {

              List<Person> persons = personService.getScrollData(0, 20);

              PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);

              listView.setAdapter(adapter);

       }

 

       private void show2() {

              Cursor cursor = personService.getCursorScrollData(0, 20);

              SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,

                            new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});

              listView.setAdapter(adapter);

       }

 

       private void show() {

              List<Person> persons = personService.getScrollData(0, 20);

              List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();

              for(Person person : persons){

                     HashMap<String, Object> item = new HashMap<String, Object>();

                     item.put("name", person.getName());

                     item.put("phone", person.getPhone());

                     item.put("amount", person.getAmount());

                     item.put("id", person.getId());

                     data.add(item);

              }

              SimpleAdapter adapter = n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,