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 = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
listView.setAdapter(adapter);
}
}
Adapter代码:
package cn.itcast.adapter;
import java.util.List;
import cn.itcast.db.R;
import cn.itcast.domain.Person;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class PersonAdapter extends BaseAdapter {
private List<Person> persons;//在绑定的数据
private int resource;//绑定的条目界面
private LayoutInflater inflater;
public PersonAdapter(Context context, List<Person> persons, int resource) {
this.persons = persons;
this.resource = resource;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return persons.size();//数据总数
}
@Override
public Object getItem(int position) {
return persons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView nameView = null;
TextView phoneView = null;
TextView amountView = null;
if(convertView==null){
convertView = inflater.inflate(resource, null);//生成条目界面对象
nameView = (TextView) convertView.findViewById(R.id.name);
phoneView = (TextView) convertView.findViewById(R.id.phone);
amountView = (TextView) convertView.findViewById(R.id.amount);
ViewCache cache = new ViewCache();
cache.nameView = nameView;
cache.phoneView = phoneView;
cache.amountView = amountView;
convertView.setTag(cache);
}else{
ViewCache cache = (ViewCache) convertView.getTag();
nameView = cache.nameView;
phoneView = cache.phoneView;
amountView = cache.amountView;
}
Person person = persons.get(position);
//下面代码实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return convertView;
}
private final class ViewCache{
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}
}