I am trying to update Notes in the room database in My UpdateActivity But it does not update in the database it does not give an error(Works fine) but does not update data in database. Here is UpdateActivity. My log value gives the correct result that I typed in EditText But did not update in the Database.
package com.example.keepnotes;
import static android.content.ContentValues.TAG;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.Objects;
public class UpdateActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
Toolbar toolbar_add = findViewById(R.id.toolbar_update_activity);
setSupportActionBar(toolbar_add);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
toolbar_add.setNavigationIcon(R.drawable.back_button);
toolbar_add.setNavigationOnClickListener(view -> onBackPressed());
EditText title = findViewById(R.id.update_activity_title);
EditText text = findViewById(R.id.update_activity_text);
Button updateBtn = findViewById(R.id.Update_button);
databaseHelper = DatabaseHelper.getDatabase(UpdateActivity.this);
String titleText = "";
String bodyText = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
titleText = extras.getString("title");
bodyText = extras.getString("text");
}
title.setText(titleText);
text.setText(bodyText);
updateBtn.setText(R.string.update);
Notes notes = new Notes(titleText,bodyText);
updateBtn.setOnClickListener(view -> {
notes.setTitle(title.getText().toString());
notes.setText(text.getText().toString());
databaseHelper.notesDao().updateNotes(notes);
Log.d(TAG, "onCreate: " + notes.title + notes.text);
finish();
});
}
}
I think there is any problem in initializing Notes
Notes notes = new Notes(titleText,bodyText);
I initialized like this in my activity.
here is Notes class.
package com.example.keepnotes;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
@Entity(tableName = "notesTable")
public class Notes {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "title")
public String title;
@ColumnInfo(name = "text")
public String text;
Notes(int id, String title, String text) {
this.id = id;
this.text = text;
this.title = title;
}
@Ignore
Notes(String title, String text) {
this.text = text;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
here is NotesDao class
package com.example.keepnotes;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface NotesDao {
@Query("SELECT * FROM notesTable")
List<Notes> getAllNotes();
@Query("SELECT * FROM notesTable")
LiveData<List<Notes>> findAllNotes();
@Insert
void addNotes(Notes note);
@Update
void updateNotes(Notes note);
@Delete
void deleteNotes(Notes note);
}
How can I resolve this?