Android - 添加到 SQLite 数据库后不会更新 ListView,除非重新启动应用程序
Posted
技术标签:
【中文标题】Android - 添加到 SQLite 数据库后不会更新 ListView,除非重新启动应用程序【英文标题】:Android - ListView not being updated after adding to SQLite database unless app is restarted 【发布时间】:2015-11-04 08:07:33 【问题描述】:我目前正在尝试通过查询为适配器提供数据的 SQLite 数据库来填充 ListView,我可以通过添加条目来更新该数据库。但是,除非我重新启动应用程序,否则 ListView 不会更新。
这是我的数据源代码:
public class DataSource
// Database fields
private SQLiteDatabase database;
private SQLHelper dbHelper;
private String[] allColumns = null;
public DataSource(Context context)
dbHelper = new SQLHelper(context);
public void open() throws SQLException
database = dbHelper.getWritableDatabase();
public void close()
dbHelper.close();
public Person addPerson(String firstname, String lastname)
ContentValues values = new ContentValues();
values.put(SQLHelper.COLUMN_FIRSTNAME, firstname);
values.put(SQLHelper.COLUMN_LASTNAME, lastname);
long insertId = database.insert(SQLHelper.TABLE_PEOPLE, null, values);
Cursor cursor = database.query(SQLHelper.TABLE_PEOPLE, allColumns, SQLHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Person newPerson = cursorToPerson(cursor);
cursor.close();
return newPerson;
public List<Person> getAllPeople()
List<Person> people = new ArrayList<Person>();
Cursor cursor = database.query(SQLHelper.TABLE_PEOPLE, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
Person person = cursorToPerson(cursor);
people.add(person);
cursor.moveToNext();
cursor.close();
return people;
private Person cursorToPerson(Cursor cursor)
if(cursor.getCount() > 0)
Person person = new Person();
person.setId(cursor.getLong(0));
person.setFirstname(cursor.getString(1));
person.setLastname(cursor.getString(1));
return person;
return null;
这里是 SQLHelper:
public class SQLHelper extends SQLiteOpenHelper
// Database
private static final String DATABASE_NAME = "people.db";
private static final int DATABASE_VERSION = 1;
// Table
public static final String TABLE_PEOPLE = "people";
// Columns
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRSTNAME = "first_name";
public static final String COLUMN_LASTNAME = "last_name";
// Database creation sql statement
private static final String DATABASE_CREATE = "create table " + TABLE_PEOPLE + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_FIRSTNAME + " text not null, " + COLUMN_LASTNAME + " text not null);";
public SQLHelper(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase database)
database.execSQL(DATABASE_CREATE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
Log.w(SQLHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PEOPLE);
onCreate(db);
为了填充我的 ListView 适配器,我调用了 getAllPeople()
方法。为了添加到我的数据库中,我调用了addPerson()
方法。 ListView 在一个对话框中。
是否有任何特殊原因我必须重新启动我的应用才能使用新人更新 ListView?
【问题讨论】:
阅读谷歌的“安卓记事本教程”,你会发现使用[Simple]CursorAdapter
和自定义ContentProvider
可以轻松完成
【参考方案1】:
您必须将数据更改通知您的适配器。根据适配器的类型,您需要调用swapCursor()
或notifyDatasetChanged()
【讨论】:
【参考方案2】:您是否在每次添加人员时都调用 notifyDataSetChanged?
【讨论】:
以上是关于Android - 添加到 SQLite 数据库后不会更新 ListView,除非重新启动应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如何从android studio中的recycler视图将数据添加到SQLite数据库中