mercredi 28 octobre 2015

SQLite in Android How to update a row?

This is my code in my DatabaseHandler, i can't seems to update a row to update my gold after i've purchase the item

    public int updateGold(Stats s) {
    SQLiteDatabase db = this.getWritableDatabase(); `   

    ContentValues values = new ContentValues();
    values.put(KEY_GOLD, s.getGold());
    // updating row
   return db.update(TABLE_STATS, values, KEY_ID + " = ?",
           new String[] { String.valueOf(s.getId()) });
}

This is my another class to make the purchase of the item where i call the update database

int cGold, aGold;
    if (spinner.getSelectedItemPosition() == 0) {
        getGoldstatus = db.getGold();

        for (Stats cn : getGoldstatus) {
            if (Integer.parseInt(cn.getGold()) >= 200) {
                Toast.makeText(this, "You have purchase! ", Toast.LENGTH_SHORT).show();
                cGold = Integer.parseInt(cn.getGold());
                aGold = cGold - 200;
                showGold.setText(aGold+"");
                db.updateGold(new Stats(aGold+""));
            }
            else
            {
                Toast.makeText(this, "You have not enough gold! ", Toast.LENGTH_SHORT).show();
            }

        }

there is no error but the program doesn't work after i click "Buy" and check the row of my database it is still not updated. pls help

Aucun commentaire:

Enregistrer un commentaire