您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
32_cursoradapter的原理和使用_注意_id的问题
发布时间:2021-02-20 15:16:31编辑:雪饮阅读()
继上次使用了数组数据适配器进行视图渲染,那么视图渲染还可以用简单游标适配器进行渲染,既然需要用游标去渲染,则需要一个将数据库中数据查询出来不做任何处理,仅仅以数据结果集进行返回的方法。
则在前面的PersonDao.java中新增方法如:
package com.example.myapplication;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
public class PersonDao {
private static final String TAG = "PersonDao";
private MyDBOpenHelper dbOpenHelper;
public PersonDao(Context context) {
dbOpenHelper = new MyDBOpenHelper(context);
}
/**
* 添加一条记录
*/
public void add(String name, int age) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
db.insert("person", null, values);
db.close();
}
}
/**
* 数据库的查询操作
*/
public boolean find(String name) {
boolean result = false;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.query("person", null, "name=?",
new String[] { name }, null, null, null);
if (cursor.moveToFirst()) {
result = true;
}
cursor.close();
db.close();
}
return result;
}
/**
* 删除一条记录
*/
public void delete(String name) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
db.delete("person", "name=?", new String[] { name });
db.close();
}
}
/**
* 数据库的更改操作
*/
public void update(String name, String newname, int newage) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", newname);
values.put("age", newage);
db.update("person", values, "name=?", new String[] { name });
db.close();
}
}
/**
* 查询所有信息
*/
public List<Person> findAll() {
List<Person> persons = null;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.query("person", null, null, null, null, null,
null);
persons = new ArrayList<Person>();
while (cursor.moveToNext()) {
Person person = new Person();
String name = cursor.getString(cursor.getColumnIndex("name"));
person.setName(name);
int age = cursor.getInt(cursor.getColumnIndex("age"));
person.setAge(age);
persons.add(person);
}
cursor.close();
db.close();
}
return persons;
}
/**
* 查询所有信息
*/
public Cursor findAllbyCursor() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.rawQuery("select personid,age,name from person", null);
return cursor;
// 注意了 一定不要把数据库 关闭了
//因为结果集去渲染视图,若关闭了数据库则结果集就释放了,则后果你懂得
}
return null;
}
/**
* 银行转账的方法
*/
public void transaction() {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
try {
db.beginTransaction();
db.execSQL("update person set account=? where name=?",
new Object[] { 1000, "zhangsan98" });
db.execSQL("update person set account=account-? where name=?",
new Object[] { 200, "zhangsan98" });
db.execSQL("update person set account=? where name=?",
new Object[] { 0, "lisi98" });
db.execSQL("update person set account=account+? where name=?",
new Object[] { 200, "lisi98" });
db.setTransactionSuccessful();
}
catch (Exception e) {}
finally {
db.endTransaction();
db.close();
}
}
}
}
那么要在MainActivity.java中进行渲染操作,则如:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.List;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private List<Person> persons;
private LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// inflater 是系统的一个服务 初始化服务(布局填充服务)
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
// 第一步得到 组件的id的引用
mListView = (ListView) this.findViewById(R.id.lv_all_person);
PersonDao dao = new PersonDao(this);
persons = dao.findAll();
//用简单游标适配器渲染视图
Cursor c = dao.findAllbyCursor();
mListView.setAdapter(new SimpleCursorAdapter(this, R.layout.item, c,
new String[] { "name", "age" }, new int[] { R.id.tv_name,
R.id.tv_age }));
//实现listview中的条目的点击事件
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person object = (Person) parent.getItemAtPosition(position);
String name = object.getName();
Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
}
});
}
}
那么当我们准备部署运行到设备中时候日志猫给我们了如下提示:
大概意思是说_id字段不存在,可是我们明明没有select _id这个字段啊?
原来人家默认是从结果集中来取这个_id字段,而我们没有select _id字段,所以自然就会出现该错误了,但是我们数据表中也没有_id这个字段,我们倒是有一个类似_id的字段是personid字段,那么我们别名下即可,于是乎PersonDao.java:
package com.example.myapplication;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
public class PersonDao {
private static final String TAG = "PersonDao";
private MyDBOpenHelper dbOpenHelper;
public PersonDao(Context context) {
dbOpenHelper = new MyDBOpenHelper(context);
}
/**
* 添加一条记录
*/
public void add(String name, int age) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
db.insert("person", null, values);
db.close();
}
}
/**
* 数据库的查询操作
*/
public boolean find(String name) {
boolean result = false;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.query("person", null, "name=?",
new String[] { name }, null, null, null);
if (cursor.moveToFirst()) {
result = true;
}
cursor.close();
db.close();
}
return result;
}
/**
* 删除一条记录
*/
public void delete(String name) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
db.delete("person", "name=?", new String[] { name });
db.close();
}
}
/**
* 数据库的更改操作
*/
public void update(String name, String newname, int newage) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
ContentValues values = new ContentValues();
values.put("name", newname);
values.put("age", newage);
db.update("person", values, "name=?", new String[] { name });
db.close();
}
}
/**
* 查询所有信息
*/
public List<Person> findAll() {
List<Person> persons = null;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.query("person", null, null, null, null, null,
null);
persons = new ArrayList<Person>();
while (cursor.moveToNext()) {
Person person = new Person();
String name = cursor.getString(cursor.getColumnIndex("name"));
person.setName(name);
int age = cursor.getInt(cursor.getColumnIndex("age"));
person.setAge(age);
persons.add(person);
}
cursor.close();
db.close();
}
return persons;
}
/**
* 查询所有信息
*/
public Cursor findAllbyCursor() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.rawQuery("select personid as _id,age,name from person", null);
return cursor;
// 注意了 一定不要把数据库 关闭了
//因为结果集去渲染视图,若关闭了数据库则结果集就释放了,则后果你懂得
}
return null;
}
/**
* 银行转账的方法
*/
public void transaction() {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
if (db.isOpen()) {
try {
db.beginTransaction();
db.execSQL("update person set account=? where name=?",
new Object[] { 1000, "zhangsan98" });
db.execSQL("update person set account=account-? where name=?",
new Object[] { 200, "zhangsan98" });
db.execSQL("update person set account=? where name=?",
new Object[] { 0, "lisi98" });
db.execSQL("update person set account=account+? where name=?",
new Object[] { 200, "lisi98" });
db.setTransactionSuccessful();
}
catch (Exception e) {}
finally {
db.endTransaction();
db.close();
}
}
}
}
于是乎我们再次跑成功了
关键字词:cursoradapter,android