Reputation: 802
I am currently trying to populate a ListView by querying a SQLite database which feeds the adapter, which I can update by adding an entry. However, the ListView doesn't get updated unless I restart the app.
Here is my data source code:
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;
}
}
And here is 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);
}
}
To populate my ListView adapter, I call the getAllPeople()
method. To add to my database, I call the addPerson()
method. The ListView is inside a dialog.
Is there any particular reason why I must restart my app for the ListView to be updated with the new person?
Upvotes: 0
Views: 730
Reputation: 75629
You must notify your adapter about data changed. Depending on type of adapter you need to either call swapCursor()
or notifyDatasetChanged()
Upvotes: 3
Reputation: 387
Did you call notifyDataSetChanged everytime you add a Person?
Upvotes: 1