dimanche 13 septembre 2015

Updating a record in SQLite using a Spinner value as the WHERE clause

I have this:

enter image description here

I have a spinner that provides choices of employees taken from SQLite database. I'm trying to update the record by using the option selected as the where value. For example, if Lysandros Lysandrou is selected, then that record with the corresponding name should be updated in the database.

My research has got me here so far:

The code is working but no actual update is happening. How can I achieve the update?

My spinner:

private void loadSpinnerData() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
    //spinner drop down elements
    List<DataBean> list = db.getAllDat();
    String[] nameList = new String[list.size()];

    for (int i=0; i<list.size(); i++) {
        nameList[i] = list.get(i).getName() + " " + list.get(i).getSurname();

    }
    //creating adapter for spinner
    ArrayAdapter<String > dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, nameList);
    //drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //attaching data adapter to spinner
    SelectName.setAdapter(dataAdapter);
}

My update method:

public int updateEmployee(String nameupdate,
                          String surnameupdate,
                          String departmentupdate,
                          String workplaceupdate) {

    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(LysandrosHelper.NAME, nameupdate);
    contentValues.put(LysandrosHelper.SURNAME, surnameupdate);
    contentValues.put(LysandrosHelper.DEPARTMENT, departmentupdate);
    contentValues.put(LysandrosHelper.WORKPLACE, workplaceupdate);

    return db.update(LysandrosHelper.TABLE_NAME, contentValues, LysandrosHelper.NAME + " = ?",
            new String[] {String.valueOf(DataBean.class.getName())});
}

My DataBean class:

public class DataBean {

//employee fields
protected int id;
protected String name;
protected String surname;
protected String department;
protected String workplace;

public DataBean (int id, String name, String surname, String department, String workplace ) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;
}

public DataBean (String name, String surname, String department, String workplace, String absentName, String absenceStartdate, String absenceEnddate, String absenceNotes) {
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;

}

public int getID() {
    return this.id;
}

public int setID(int id) { return this.id = id; }

public String getName() {
    return this. name;
}

public String getSurname() {
    return this.surname;
}

public String getDepartment() {
    return this.department;
}

public String getWorkplace() {
   return this.workplace;
}

Please bear with me as I'm new to Android and Java. What am I missing? I'm thinking it might be something with my WHERE clause but I can't know. Also, I think I've included everything needed but if you require anything else please let me know.

Aucun commentaire:

Enregistrer un commentaire