Issue
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?
Solution
As mentioned in the comments, you will need the primary key (probably an integral id) of the Notes
entry you want to update.
It looks like you are using putExtra
to pass information about an existing Notes
object that lives in a different Activity
. If that existing Notes
object (call it noteToUpdate
) was retrieved from the database, it should contain the primary key you need. You can add another key/value pair to the Intent
in the original Activity
with something like putExtra("id", noteToUpdate.id)
. Then, in the UpdateActivity
, you can use something along the lines of noteId = extras.getString("id")
to retrieve the id.
Once you have this noteId
, you can replace Notes notes = new Notes(titleText, bodyText);
with a call to a constructor that accepts an id. Alternatively, you could write something like notes.setId(noteId)
after the object is constructed. With the primary key now set correctly, the updateNotes
call should work.
Edit: if I misinterpreted your comments and there is no id field (or similar), please share the Notes
entity structure and primary key.
Answered By - tenuki
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.