SQLite Updating A Row And Updating Listview Created By Cursor
Solution 1:
Hi in your Database Handler Class you need to create methods for update..
as I can see you have an uto incremented primary key (INTEGER PRIMARY KEY AUTOINCREMENT) which is good since we will be using this primary key to locate the record and do the update. to the update method you pass a new Details object which contains new data.
The method in the Database Handler class should be like so :
//Updates the no of comments
public void updateDetails(Details details){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//Take new values from the details object provided
values.put(COLUMN_FIRSTNAME, details.getFirstname());
values.put(COLUMN_SURNAME, details.getSurname());
values.put(COLUMN_PHONE, details.getPhone());
values.put(COLUMN_EMAIL, details.getEmail());
values.put(COLUMN_ADDRESS1, details.getAddress1());
values.put(COLUMN_ADDRESS2, details.getAddress2());
//Update the details object where id matches
db.update(TABLE_DETAILS, values, COLUMN_ID + "='" + details._id + "'", null);
db.close();
}
Update
Basically you create a new details object with the new data entered from the user and you pass it to the update details method in the dbHandler class, it is important that you set the id to 1 since the sql query will look for that particular detail in the database :
Details details = new Details();
details.set_id(1);
details.setFirstname("Steve");
dbHandler.updateDetails(details);
No you have the record updated ..please try and advice
Solution 2:
Instead of an ArrayAdapter try using a SimpleCursorAdapter
. Check here for more info on usage.
Also use Loaders to do all this in the background and notify the adapter on changes
Post a Comment for "SQLite Updating A Row And Updating Listview Created By Cursor"