mercredi 6 janvier 2016

Android: Update Data

I'm having a problem with my codes in updating data. I searched and apply those codes that I get but when I run the app and click the button update nothing happen with the data in the database. Please check my codes.

My DatabaseHelper class:

public int updateData(String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4, SQLiteDatabase sqLiteDatabase ) {
    ContentValues contentValues = new ContentValues();

    contentValues.put(AdminContact.Question.COLUMN_QUESTION, new_question);
    contentValues.put(AdminContact.Question.COLUMN_ANSWER1, new_ans1);
    contentValues.put(AdminContact.Question.COLUMN_ANSWER2, new_ans2);
    contentValues.put(AdminContact.Question.COLUMN_ANSWER3, new_ans3);
    contentValues.put(AdminContact.Question.COLUMN_ANSWER4, new_ans4);

    String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";
    String[] selection_arg = {new_question};
    int count = sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ,contentValues,selection,selection_arg);

    return count;
}

My Update class:

public void updateData(View view) {
    helper = new DatabaseHelper(this);
    sqLiteDatabase = helper.getWritableDatabase();

    String question, ans1, ans2, ans3, ans4;

    question = New_Question.getText().toString();
    ans1 = New_Ans1.getText().toString();
    ans2 = New_Ans2.getText().toString();
    ans3 = New_Ans3.getText().toString();
    ans4 = New_Ans4.getText().toString();
    int count = helper.updateData(question,ans1,ans2,ans3,ans4,sqLiteDatabase);
    Toast.makeText(getApplicationContext(),count + " Successfully Updated", Toast.LENGTH_LONG).show();
    finish();
}

Aucun commentaire:

Enregistrer un commentaire