Ajaya Shrestha
Ajaya Shrestha

Reputation: 23

Database is not updating data from one activity to another

I need to pass data from DeleteLayout to update data in historyActivity which will update once I edit the data.It does not throw any error but my data is not updating after editing. What can be done to pass data between activities and update the database.

My updateDatamethod in DatabaseHelper looks like this:

    public boolean updateData(String goalDate, String goalWeight, String currentWeight){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
        contentValues.put(Col2, goalDate);
        contentValues.put(Col3, goalWeight);
        contentValues.put(Col4, currentWeight);
        db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
        return true;
    }

Deletelayout

   protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deletelayout);
        GoalDate = findViewById(R.id.goalDate2);
        CurrentWeight = findViewById(R.id.weighGoal2);
        GoalWeight = findViewById(R.id.weightCurrent2);
        goalD = findViewById(R.id.goalDateInput);
        goalW = findViewById(R.id.goalWeightInput);
        currentW = findViewById(R.id.currentWeightInput);

        imageView = findViewById(R.id.imageShow);
        data = myDB.getListContents();
        deleteB = findViewById(R.id.deleteB);
        updateB = findViewById(R.id.updateButton);
        // Entered information from the user are set to the respective variables.
        int numRows = data.getCount();
        j = new Intent(DeleteLayout.this, historyActivity.class);

        if (numRows == 0) {
            Toast.makeText(DeleteLayout.this, "Nothing in the db", Toast.LENGTH_LONG).show();
        } else {
            int i = 0;
            while (data.moveToNext()) {
                user = new User(data.getString(1), data.getString(2), data.getString(3), data.getString(4), data.getString(5));
                gWt = user.getGoalWeight();
                cWt = user.getCurrentWeight();
                gDt = user.getGoalDate();
                GoalDate.setText(gDt);
                GoalWeight.setText(gWt);
                CurrentWeight.setText(cWt);

                deleteB.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDB.deleteContent(GoalDate.getText().toString(), GoalWeight.getText().toString(), CurrentWeight.getText().toString());
                        startActivity(j);
                    }
                });
                updateB.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        boolean myUpdate = myDB.updateData(goalD.getText().toString(), goalW.getText().toString(), currentW.getText().toString());
                        if(myUpdate == true){
                            Toast.makeText(DeleteLayout.this, "Data updated", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(DeleteLayout.this, "Data not updated", Toast.LENGTH_SHORT).show();

                        }
                        startActivity(j);
                    }
                });
            }

        }

    }

Upvotes: 1

Views: 47

Answers (1)

MikeT
MikeT

Reputation: 57103

Issues

One issue you have is that you are not asking for any changes to be made.

Additionally you are always assuming that a row or rows have always been update, i.e. you are not checking to see if any updates have been performed.

  • The SQliteDatabase update method returns an int that has the number of rows that have been updated. If none are updated then it will return 0.

Explanation

Consider that goalDate is 2019-01-01 and goalWeight is 100 and currentWeight is 200, then you are saying

  • update
    • goalDate to 2019-01-01 and
    • goalWeight to 100 and
    • currentWeight to 200
  • for the row(s) where
    • goalDate is 2019-01-01 and
    • goalWeight is 100 and
    • currentWeight is 200

Fix

Instead of :-

public boolean updateData(String goalDate, String goalWeight, String currentWeight){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
    contentValues.put(Col2, goalDate);
    contentValues.put(Col3, goalWeight);
    contentValues.put(Col4, currentWeight);
    db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
    return true;
}

What you want is something along the lines of :-

public boolean updateData(String goalDate, String goalWeight, String currentWeight, String newGoaldate, String newGoalWeight, String newCurrentWeight){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
    contentValues.put(Col2, newGoalDate);
    contentValues.put(Col3, newGoalWeight);
    contentValues.put(Col4, newCurrentWeight);
    if (db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight}) > 0) {
        return true;
    }
    return false;
}
  • The first 3 parameters are the original values (used to locate the row to be updated), the second set are the values to be used to update the located row or rows).

Extra

Ideally you should also change :-

 String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";

to :-

String where = Col2 + "=? AND " + Col3 + "=?  AND " + Col4 +"=?";

Upvotes: 1

Related Questions