Skip to content Skip to sidebar Skip to footer

Sqlite Updating A Row And Updating Listview Created By Cursor

I have a from that a user fills in and their data is stored in a row using SQLite, a cursor gets their details from the table and outputs it in a listView. I want the user to be a

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 commentspublicvoidupdateDetails(Details details){

    SQLiteDatabasedb=this.getWritableDatabase();

    ContentValuesvalues=newContentValues();

    //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 :

Detailsdetails=newDetails();

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"