I'm new to Room and am having some trouble updating the database. Insert and delete work as expected but the update does not. The data remains the same in the database. I've tried the update using the entire object and the specific field in an #Query statement. What am I doing wrong?
Entity:
#Entity(tableName = "Appointments")
public class Appointment {
#PrimaryKey
#NonNull
public int AppointmentID;
public Boolean CheckInStatus;
public Appointment(
#NonNull int AppointmentID,
Boolean CheckInStatus
) {
this.AppointmentID = AppointmentID;
this.CheckInStatus = CheckInStatus;
}
public void setCheckInStatus(Boolean checkInStatus) {
CheckInStatus = checkInStatus;
}
#NonNull
public int getAppointmentID() {
return this.AppointmentID;
}
public Boolean getCheckInStatus() {
return CheckInStatus;
}
public void setID(#NonNull int AppointmentID) {
this.AppointmentID = AppointmentID;
}
Dao:
#Update
void update(Appointment appointment);
#Query("UPDATE Appointments SET CheckInStatus = 1 WHERE AppointmentID = :Id")
void updateCheckIn(int Id);
Repo:
public void updateCheckIn(int Id) {
new AppointmentRepository.updateCheckInAsyncTask(mAppointmentDao, Id);
}
public static class updateCheckInAsyncTask extends AsyncTask<Void, Void, Void> {
private AppointmentDao mAsyncTaskDao;
private int mId;
updateCheckInAsyncTask(AppointmentDao dao, int Id) {
mAsyncTaskDao = dao;
mId = Id;
}
#Override
protected Void doInBackground(Void... voids) {
mAsyncTaskDao.updateCheckIn(mId);
return null;
}
}
public void update(Appointment appointment) {
new AppointmentRepository.updateAsyncTask(mAppointmentDao, appointment);
}
private static class updateAsyncTask extends AsyncTask<Void, Void, Void> {
private AppointmentDao mAsyncTaskDao;
private Appointment mAppointment;
updateAsyncTask(AppointmentDao dao, Appointment appointment) {
mAsyncTaskDao = dao;
mAppointment = appointment;
}
#Override
protected Void doInBackground(Void... voids) {
mAsyncTaskDao.update(mAppointment);
return null;
}
}
View Model:
public void update(Appointment appointment) { mRepository.update(appointment); }
public void updateCheckIn(int Id) { mRepository.updateCheckIn(Id); }
Activity:
// Update check in status in table
mAppointmentVM.updateCheckIn(mId);
The correct id is being passed as was the updated object when I went that route. Any assistance is greatly appreciated.
EDIT:
The final answer is:
public void updateCheckIn(int Id) {
new AppointmentRepository.updateCheckInAsyncTask(mAppointmentDao, Id).execute();
}
public static class updateCheckInAsyncTask extends AsyncTask<Void, Void, Void> {
private AppointmentDao mAsyncTaskDao;
private int mId;
updateCheckInAsyncTask(AppointmentDao dao, int Id) {
mAsyncTaskDao = dao;
mId = Id;
}
#Override
protected Void doInBackground(Void... voids) {
Appointment entity = mAsyncTaskDao.getAppointmentDetails(mId);
entity.setCheckInStatus(true);
mAsyncTaskDao.update(entity);
return null;
}
}
You could simply do it like that:
Get item that you want to update from DB;
Update that POJO by setting CheckInStatus with setter method;
Simply use #Update(onConflict = OnConflictStrategy.REPLACE) function and pass your updated POJO to it (because Id's match and its a Primary key, DB will automatically identify this situation and update all required fields for required object);
**Example:**
#Override
protected Void doInBackground(Void... voids) {
Appointment entity = mAsyncTaskDao.getAppointmentById(mId);
entity.setCheckInStatus(true);
mAsyncTaskDao.update(entity);
return null;
}
That is it :) Good luck.
Related
I am writing a simple application that stores 2 string values in a database using Room (I am trying to learn this library). So, I have only one line on my list. Presented for the first time. The rest are not displayed. What is the reason for this behavior?
Model
#Entity
public class Note {
#PrimaryKey(autoGenerate = true)
private long id;
private String title;
private String text;
public long getId() {
return id;
}
public void setId(long 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;
}
}
NoteDao
#Dao
public interface NoteDao {
#Insert
void insert(Note note);
#Delete
void delete(Note note);
#Query("SELECT * FROM Note")
List<Note> getAllNotes();
}
AppDatabase
#Database(entities = Note.class, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract NoteDao getNoteDao();
}
DataManager
public class DataManager {
private AppDatabase appDatabase;
public DataManager (AppDatabase appDatabase) {
this.appDatabase = appDatabase;
}
public List <Note> getNotes () {
try {
return new GetNotes (). execute (). get ();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace ();
return null;
}
}
public void insertNote (Note note) {
new InsertNote (note) .execute ();
}
public void deleteNote (Note note) {
new DeleteNote (note) .execute ();
}
// Get all notes
public class GetNotes extends AsyncTask <Void, Void, List <Note>> {
#Override
protected List <Note> doInBackground (Void ... voids) {
return appDatabase.getNoteDao (). getAllNotes ();
}
}
// Insert note
public class InsertNote extends AsyncTask <Void, Void, Void> {
private Note note;
InsertNote (Note note) {
this.note = note;
}
#Override
protected Void doInBackground (Void ... voids) {
appDatabase.getNoteDao (). insert (note);
return null;
}
}
// Delete note
public class DeleteNote extends AsyncTask <Void, Void, Void> {
private Note note;
public DeleteNote (Note note) {
this.note = note;
}
#Override
protected Void doInBackground (Void ... voids) {
appDatabase.getNoteDao (). delete (note);
return null;
}
}
}
Mvp
public interface NoteListView extends MvpView {
void showNoteList(List<Note> note);
}
Presenter
public class NoteListPresenter extends MvpPresenter<NoteListView> {
private DataManager dataManager;
public NoteListPresenter(DataManager dataManager) {
this.dataManager = dataManager;
}
public void getNotes(){
getView().showNoteList(dataManager.getNotes());
}
public void deleteNote(Note note) {
dataManager.deleteNote(note);
getView().showNoteList(dataManager.getNotes());
}
}
Adapter
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder> {
private List<Note> listNote;
private Context context;
public NoteAdapter(Context context, List<Note> listNote) {
this.listNote = listNote;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(listNote.get(position).getTitle());
holder.text.setText(listNote.get(position).getText());
}
#Override
public int getItemCount() {
return listNote.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title, text;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements NoteListView {
private NoteListPresenter presenter;
private RecyclerView recyclerView;
private NoteAdapter noteAdapter;
private ConstraintLayout constraintLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = findViewById(R.id.coordinatorMain);
recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
presenter = new NoteListPresenter(App.getDataManager());
presenter.attachView(this);
presenter.getNotes();
}
public void onNextActivity(View view){
startActivity(new Intent(MainActivity.this, AddNoteActivity.class));
}
#Override
public void showNoteList(List<Note> note) {
noteAdapter = new NoteAdapter(this, note);
recyclerView.setAdapter(noteAdapter);
}
#Override
protected void onResume() {
super.onResume();
presenter.getNotes();
}
#Override
public void showMessage(String message) {
Snackbar.make(constraintLayout, message, Snackbar.LENGTH_SHORT).show();
}
}
UPD
SaveNoteView
public interface SaveNoteView extends MvpView {
void insertNote(Note note);
}
SaveNotePresenter
public class SaveNotePresenter extends MvpPresenter<SaveNoteView> {
private DataManager dataManager;
public SaveNotePresenter(DataManager dataManager) {
this.dataManager = dataManager;
}
public void insertNote(Note note){
dataManager.insertNote(note);
getView().insertNote(note);
}
}
AddNoteActivity
public class AddNoteActivity extends AppCompatActivity implements SaveNoteView {
private TextInputEditText title, text;
private ConstraintLayout constraintLayout;
private SaveNotePresenter presenter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
constraintLayout = findViewById(R.id.constrainAdd);
title = findViewById(R.id.titleEditText);
text = findViewById(R.id.textEditText);
presenter = new SaveNotePresenter(App.getDataManager());
presenter.attachView(this);
}
//Save a note
public void saveNote(View view){
Note note = new Note();
note.setTitle(title.getText().toString());
note.setText(text.getText().toString());
presenter.insertNote(note);
finish();
}
#Override
public void insertNote(Note note) {
DataManager dataManager = App.getDataManager();
dataManager.insertNote(note);
}
#Override
public void showMessage(String message) {
Snackbar.make(constraintLayout, message, Snackbar.LENGTH_SHORT).show();
}
}
It's pretty likely that there is only 1 id which is beeing replaced the whole time (if this is not the case, check how often insertion is done)
I used Kotlin with Room but in this official example they have a public autogenerated primaryKey, which might be required for Room to access it and autogenerate it. So make the variable public and see if it works
There was no problem. I just specified the parent layout for item_list in full screen. Therefore, the subsequent ones were not visible. Silly mistake:D
In my code below I'm getting rowId. I've read that it's also possible to get the last inserted row id from #Insert directly. In my code I changed void insert to long and tried many other things as in examples I found on the internet, but every time I get errors. Would you like to provide me a code/solution to get the row/user ID from #Insert?
#Dao
public interface UserDao {
#Insert
void insert(UserEntity userEntity);
#Update
void update(UserEntity userEntity);
#Delete
void delete(UserEntity userEntity);
#Query("DELETE FROM user_table")
void deleteAllUsers();
#Query("SELECT * FROM user_table")
LiveData<List<UserEntity>> getAllUsers();
// ====== from here ======
#Query("SELECT * FROM user_table")
LiveData<UserEntity> getRowId();
// ====== till here ======
}
Entity
#Entity(tableName = "user_table")
public class UserEntity {
#PrimaryKey(autoGenerate = true)
private int id;
private String userName;
private String userTelephoneNumber;
public UserEntity(String userName, String userTelephoneNumber) {
this.userName = userName;
this.userTelephoneNumber = userTelephoneNumber;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getUserName() {
return userName;
}
public String getUserTelephoneNumber() {
return userTelephoneNumber;
}
}
Repository
public class UserRepository {
private UserDao userDao;
private LiveData<List<UserEntity>> allUsers;
public UserRepository(Application application) {
HandymanDatabase handymanDatabase = HandymanDatabase.getInstance(application);
userDao = handymanDatabase.userDao();
allUsers = userDao.getAllUsers();
}
public void insert(UserEntity userEntity) {
new InsertUserAsyncTask(userDao).execute(userEntity);
}
public void update(UserEntity userEntity) {
new UpdateUserAsyncTask(userDao).execute(userEntity);
}
public void delete(UserEntity userEntity) {
new DeleteUserAsyncTask(userDao).execute(userEntity);
}
public void deleteAllUsers() {
new DeleteAllUsersAsyncTask(userDao).execute();
}
public LiveData<List<UserEntity>> getAllUsers() {
return allUsers;
}
// ====== from here ======
public LiveData<UserEntity> getRowId() {
return userDao.getRowId();
}
// ====== till here ======
private static class InsertUserAsyncTask extends AsyncTask<UserEntity, Void, Void> {
private UserDao userDao;
private InsertUserAsyncTask(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected Void doInBackground(UserEntity... userEntities) {
userDao.insert(userEntities[0]);
return null;
}
}
private static class UpdateUserAsyncTask extends AsyncTask<UserEntity, Void, Void> {
private UserDao userDao;
private UpdateUserAsyncTask(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected Void doInBackground(UserEntity... userEntities) {
userDao.update(userEntities[0]);
return null;
}
}
private static class DeleteUserAsyncTask extends AsyncTask<UserEntity, Void, Void> {
private UserDao userDao;
private DeleteUserAsyncTask(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected Void doInBackground(UserEntity... userEntities) {
userDao.delete(userEntities[0]);
return null;
}
}
private static class DeleteAllUsersAsyncTask extends AsyncTask<Void, Void, Void> {
private UserDao userDao;
private DeleteAllUsersAsyncTask(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected Void doInBackground(Void... voids) {
userDao.deleteAllUsers();
return null;
}
}
}
ViewModel
public UserViewModel(#NonNull Application application) {
super(application);
userRepository = new UserRepository(application);
allUsers = userRepository.getAllUsers();
}
public void insert(UserEntity userEntity) {
userRepository.insert(userEntity);
}
public void update(UserEntity userEntity) {
userRepository.update(userEntity);
}
public void delete(UserEntity userEntity) {
userRepository.delete(userEntity);
}
public void deleteAllUsers() {
userRepository.deleteAllUsers();
}
public LiveData<List<UserEntity>> getAllUsers() {
return allUsers;
}
// ====== from here ======
public LiveData<UserEntity> getRowId() {
return userRepository.getRowId();
}
// ====== till here ======
}
Fragment/Activity
public class UserFavoritesFragment extends Fragment {
private static final String TAG = "UserFavoritesFragment";
private UserViewModel userViewModel;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = getLayoutInflater().inflate(R.layout.fragment_user_favorites, container, false);
final TextView textViewUserName = view.findViewById(R.id.textViewUserName);
TextView textViewUserPhone = view.findViewById(R.id.textViewUserPhone);
userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
// ====== from here ======
userViewModel.getRowId().observe(getViewLifecycleOwner(), new Observer<UserEntity>() {
#Override
public void onChanged(UserEntity userEntity) {
long rowId = userEntity.getId();
Log.d(TAG, "onChanged: " + rowId);
}
});
// ====== till here ======
return view;
}
}
You can do that using a listener interface that has a callback that accepts a long value of the inserted row id in the database.
Listener Interface
public interface NewIdListener {
void onInsert(long id);
}
Dao
#Dao
public interface UserDao {
#Insert
long insert(UserEntity userEntity); // return id of the inserted userEntity
}
Repository
public class UserRepository {
private Executor mExecutor = Executors.newSingleThreadExecutor();
private UserDao userDao;
...
public void insertUserEntity(final UserEntity entity, final NewIdListener listener) {
mExecutor.execute(new Runnable() {
#Override
public void run() {
listener.onInsert(userDao.insert(entity));
}
});
}
ViewModel
public void insertUserEntity(UserEntity entity, NewIdListener listener) {
userRepository.insertUserEntity(entity, listener);
}
Activity
userViewModel.insertUserEntity(new UserEntity("User Name", "12345678"), new NewIdListener() {
#Override
public void onInsert(final long id) {
requireActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(requireActivity(), "Id: " + id, Toast.LENGTH_SHORT).show();
}
});
}
});
Note: For background thread, I've used Executor instead of AsyncTask as AsyncTask is deprecated now.
Where I am
I am attempting to fill a spinner with data from a database, using Room. The data is a list of terms, which have courses associated with them.
I want to use the spinner when creating a new course, to select a term to associate it with.
Currently, the spinner does not show a default option, but if you click the spinner it shows a list of the data to select from. After you select something from the spinner, it does not show what you have selected.
Here is my code for loading data into the spinner's adapter:
termsList = new ArrayList<>();
termIdList = new ArrayList<>();
mTermViewModel = new ViewModelProvider(this).get(TermViewModel.class);
mTermViewModel.getAllTerms().observe(this, new Observer<List<TermEntity>>() {
#Override
public void onChanged(#Nullable final List<TermEntity> terms) {
for (TermEntity term : terms) {
termsList.add(term.getTermName());
termIdList.add(term.getTermId());
}
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, termsList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mTermSpinner.setAdapter(adapter);
}
Here is the TermDAO
#Dao
public interface TermDAO {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(TermEntity term);
#Query("DELETE FROM terms")
void deleteAllTerms();
#Query("SELECT * FROM terms ORDER BY termId ASC")
LiveData<List<TermEntity>> getAllTerms();
#Query("SELECT * FROM terms WHERE termId = :termId")
LiveData<List<TermEntity>> getTerm(int termId);
#Query("SELECT termId FROM terms WHERE termName = :termName")
LiveData<List<TermEntity>> getTermIdByTermName(String termName);
}
Here is the TermViewModel
public class TermViewModel extends AndroidViewModel {
private SchoolTrackerRepository mRepository;
private LiveData<List<TermEntity>> mAllTerms;
public TermViewModel(#NonNull Application application) {
super(application);
mRepository = new SchoolTrackerRepository(application);
mAllTerms = mRepository.getAllTerms();
}
public LiveData<List<TermEntity>> getAllTerms() {
return mAllTerms;
}
public void insert(TermEntity termEntity) {
mRepository.insert(termEntity);
}
}
I want to show the term name in the spinner and use the corresponding termId to perform the query.
What I've tried
I have tried using Mutable LiveData instead of LiveData but when I attempted to run it, I got an error that said:
Error:Not sure how to convert a Cursor to this method's return type
I'm really at a loss. Thanks for any help.
Added Repository
public class SchoolTrackerRepository {
private TermDAO mTermDAO;
private CourseDAO mCourseDAO;
int termId;
private LiveData<List<TermEntity>> mAllTerms;
private LiveData<List<CourseEntity>> mAllCourses;
private LiveData<List<CourseEntity>> mAssociatedCourses;
public SchoolTrackerRepository(Application application) {
SchoolTrackerDatabase db = SchoolTrackerDatabase.getDatabase(application);
mTermDAO = db.termDAO();
mCourseDAO = db.courseDAO();
mAllTerms = mTermDAO.getAllTerms();
mAllCourses = mCourseDAO.getAllCourses();
}
public LiveData<List<TermEntity>> getAllTerms() {
return mAllTerms;
}
public LiveData<List<CourseEntity>> getAllCourses() {
return mAllCourses;
}
public LiveData<List<CourseEntity>> getmAssociatedCourses(int termId) {
return mAssociatedCourses;
}
public void insert(TermEntity termEntity) {
new insertAsyncTask1(mTermDAO).execute(termEntity);
}
private static class insertAsyncTask1 extends AsyncTask<TermEntity, Void, Void> {
private TermDAO mAsyncTaskDAO;
insertAsyncTask1(TermDAO dao) {
mAsyncTaskDAO = dao;
}
#Override
protected Void doInBackground(final TermEntity... params) {
mAsyncTaskDAO.insert(params[0]);
return null;
}
}
public void insert(CourseEntity courseEntity) {
new insertAsyncTask2(mCourseDAO).execute(courseEntity);
}
private static class insertAsyncTask2 extends AsyncTask<CourseEntity, Void, Void> {
private CourseDAO mAsyncCourseDAO;
insertAsyncTask2(CourseDAO dao) {
mAsyncCourseDAO = dao;
}
#Override
protected Void doInBackground(final CourseEntity... params) {
mAsyncCourseDAO.insert(params[0]);
return null;
}
}
}
I guess that you missing notifyDataSetChanged after update the termsList variable.
Take a try with this:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, termsList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mTermSpinner.setAdapter(adapter);
mTermViewModel = new ViewModelProvider(this).get(TermViewModel.class);
mTermViewModel.getAllTerms().observe(this, new Observer<List<TermEntity>>() {
#Override
public void onChanged(#Nullable final List<TermEntity> terms) {
for (TermEntity term : terms) {
termsList.add(term.getTermName());
termIdList.add(term.getTermId());
}
//notifyDataSetChanged after update termsList variable here
adapter.notifyDataSetChanged();
}
});
Trying to get the insert to return the PK so I can capture it in a var and pass it to other activities.
DAO:
#Dao public interface NoteDao {
#Insert
long insert(Note note);
Repository:
private MutableLiveData<Long> noteInsertedLiveData = new MutableLiveData();
private NoteDao noteDao;
private LiveData<List<Note>> allNotes;
public NoteRepository(Application application) {
NoteDatabase database = NoteDatabase.getInstance(application);
noteDao = database.noteDao();
allNotes = noteDao.getAllNotes2();
}
public void insert(OnTaskCompleted onTaskCompleted, Note note) {
new InsertAsyncTask(noteDao, onTaskCompleted).execute(note);
}
public LiveData<Long> getNoteInsertedLiveData() {
return noteInsertedLiveData;
}
ASync:
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Long> {
private long sqCbtId = -1;
private NoteDao noteDao;
private OnTaskCompleted onTaskCompleted;
private InsertNoteAsyncTask(NoteDao noteDao, OnTaskCompleted onTaskCompleted) {
this.noteDao = noteDao;
this.onTaskCompleted = onTaskCompleted;
}
#Override
protected Long doInBackground(Note... notes) {
return noteDao.insert(notes[0])
}
#Override
protected void onPostExecute(Long result) {
onTaskCompleted.onTaskCompleted(result);
}
}
View model:
public long insert(Note note){
sqCbtId = repository.insert(note); return sqCbtId; }
Activity :
Note note = new Note(userId, therapistId, automaticThoughtString, distortions, challengeThoughtString, alternativeThoughtString, postedWorkout);
sqCbtId = noteViewModel.insert(note);
Entity:
#Entity(tableName = "note_table")
public class Note {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "cbtId")
private long sqCbtId;
#NonNull
#ColumnInfo(name = "userId")
private int userId;
#NonNull
#ColumnInfo(name = "therapistId")
private int therapistId;
#NonNull
#ColumnInfo(name = "automaticThought")
private String automaticThought;
#NonNull
#ColumnInfo(name = "distortions")
private int distortions;
#NonNull
#ColumnInfo(name = "challengeThought")
private String challengeThought;
#NonNull
#ColumnInfo(name = "alternativeThought")
private String alternativeThought;
#NonNull
#ColumnInfo(name = "postedWorkout")
private String postedWorkout;
I have one error on this line:
new InsertAsyncTask(noteDao, onTaskCompleted).execute(note)
It states that the InsertASyncTask cannot be applied to the paramteters (naoteDao, onTaskCompleted)
SQLite and hence Room's #Insert implementation returns the row id of the inserted row. If the primary key is not auto generated you already know it, provided inserted didn't return -1 which signifies error. If the Primary key is auto generated you would need to query it again.
Since your Primary Key is of type long, ie, INTEGER for SQLite, the rowId becomes the Primary Key for the row. See this: https://www.sqlite.org/rowidtable.html
Hence in this case you do not need to query again for the Primary Key. Simply use the rowId returned by #Insert (provided not -1).
Errors on your code:
sqCbtId = note.getSqCbtId(); return sqCbtId;
This is getting the sqCbtId from the object you have just inserted into the database. The id is not generated for this object. It is stored in database. Get the id from the AsyncTask's onPostExecute
Code: (ignore any compilation errors or typo, not written on IDE :P)
class ViewModel
{
private MutableLiveData<Long> noteInsertedLiveData = new MutableLiveData();
public long insert(Note note) {
new InsertNoteAsyncTask(noteDao, new InsertNoteAsyncTask.Listener {
#override
void onNoteInserted(Long sqCbtId) {
noteInsertedLiveData.setValue(sqCbtId);
}
}).execute(note);
sqCbtId = note.getSqCbtId(); return sqCbtId;
}
public LiveData<Long> getNoteInsertedLiveData() {
return noteInsertedLiveData;
}
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Long> {
private long sqCbtId = -1;
private NoteDao noteDao;
private Listener listener;
private InsertNoteAsyncTask(NoteDao noteDao, Listener listener) {
this.noteDao = noteDao;
this.listener = listener;
}
#Override
protected Long doInBackground(Note... notes) {
sqCbtId = noteDao.insert(notes[0]);
return sqCbtId;
}
#Override
protected void onPostExecute(Long result) {
listener.onNoteInserted(result);
}
interface Listener {
void onNoteInserted(Long sqCbtId);
}
}
}
On your Activity
noteViewModel.getNoteInsertedLiveData().observe(this, /** add Observer here to get update on id after every insert in db **/);
Note note = new Note(userId, therapistId, automaticThoughtString, distortions, challengeThoughtString, alternativeThoughtString, postedWorkout);
noteViewModel.insert(note);
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private long sqCbtId = -1;
private NoteDao noteDao;
private Context context;
private InsertNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
#Override
protected Void doInBackground(Note... notes) {
sqCbtId = noteDao.insert(notes[0]);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
getCbtId(sqCbtId);
Log.d(TAG, "onPostExecute: " + sqCbtId);
}
public void getCbtId(long cbtId) {
sqCbtId = cbtId;
}
}
This is the code I used which captures the row id
Hi I'm doing an app and I made the database but I don't know how to read it to show in a listview for example I have a table CategoriaEntradas and I need to show all the data in a listview but I don't understand very well how it works all the classes
This is my Dao class
#Dao
public interface CategoriaEntradasDao {
#Insert
void insert(CategoriaEntradas categoriaEntradas);
#Update
void update(CategoriaEntradas categoriaEntradas);
#Delete
void delete(CategoriaEntradas categoriaEntradas);
#Query("DELETE FROM CategoriaEntradas")
void deleteAll();
#Query("SELECT * FROM CategoriaEntradas")
LiveData<List<CategoriaEntradas>> getAll();
#Query("SELECT * FROM CategoriaEntradas WHERE IdCategoria =:Id")
LiveData<CategoriaEntradas> getOne(int Id);
}
This is my Entity class
#Entity(tableName = "CategoriaEntradas")
public class CategoriaEntradas {
#PrimaryKey(autoGenerate = true)
#NonNull
#ColumnInfo(name = "IdCategoria")
private Integer _idCategoria;
#NonNull
#ColumnInfo(name = "Name")
private String _name;
#NonNull
#ColumnInfo(name = "Image")
private String _image;
public CategoriaEntradas(#NonNull String name, #NonNull String image) { _name = name; _image = image;}
public void set_idCategoria(Integer _idCategoria){
this._idCategoria = _idCategoria;
}
#NonNull
public Integer getIdCategoria() {
return _idCategoria;
}
#NonNull
public String getName() {
return _name;
}
#NonNull
public String getImage() {
return _image;
}
}
My Repository class
public class CategoriaEntradasRepository {
private CategoriaEntradasDao categoriaEntradasDao;
private LiveData<List<CategoriaEntradas>> listLiveData;
public CategoriaEntradasRepository(Application application) {
Database db = Database.getDatabase(application);
categoriaEntradasDao = db.categoriaEntradasDao();
listLiveData = categoriaEntradasDao.getAll();
}
public LiveData<List<CategoriaEntradas>> getAll() {
return listLiveData;
}
public LiveData<CategoriaEntradas> getOne(int Id) {
return categoriaEntradasDao.getOne(Id);
}
public void insert (CategoriaEntradas categoriaEntradas) {
new CategoriaEntradasRepository.insertAsyncTask(categoriaEntradasDao).execute(categoriaEntradas);
}
private static class insertAsyncTask extends AsyncTask<CategoriaEntradas, Void, Void> {
private CategoriaEntradasDao entradasDao;
insertAsyncTask(CategoriaEntradasDao dao) {
entradasDao = dao;
}
#Override
protected Void doInBackground(final CategoriaEntradas... params) {
entradasDao.insert(params[0]);
return null;
}
}
public void update(final CategoriaEntradas categoriaEntradas){
new CategoriaEntradasRepository.updateAsyncTask(categoriaEntradasDao).execute(categoriaEntradas);
}
private static class updateAsyncTask extends AsyncTask<CategoriaEntradas, Void, Void>{
private CategoriaEntradasDao entradasDao;
updateAsyncTask(CategoriaEntradasDao dao) {
entradasDao = dao;
}
#Override
protected Void doInBackground(final CategoriaEntradas... params){
entradasDao.update(params[0]);
return null;
}
}
public void delete(final CategoriaEntradas categoriaEntradas) {
new CategoriaEntradasRepository.deleteAsyncTask(categoriaEntradasDao).execute(categoriaEntradas);
}
public void delete(final int Id) {
final LiveData<CategoriaEntradas> categoriaEntradas = getOne(Id);
if (categoriaEntradas != null) {
new CategoriaEntradasRepository.deleteAsyncTask(categoriaEntradasDao).execute(categoriaEntradas.getValue());
}
}
private static class deleteAsyncTask extends AsyncTask<CategoriaEntradas, Void, Void>{
private CategoriaEntradasDao entradasDao;
deleteAsyncTask(CategoriaEntradasDao dao) {
entradasDao = dao;
}
#Override
protected Void doInBackground(final CategoriaEntradas... params){
entradasDao.delete(params[0]);
return null;
}
}
}
And my ViewModel class
public class CategoriaEntradasViewModel extends AndroidViewModel {
private CategoriaEntradasRepository repository;
private LiveData<List<CategoriaEntradas>> listLiveData;
public CategoriaEntradasViewModel (Application application) {
super(application);
repository = new CategoriaEntradasRepository(application);
listLiveData = repository.getAll();
}
public LiveData<List<CategoriaEntradas>> getAll() { return listLiveData; }
public LiveData<CategoriaEntradas> getOne(int Id) { return repository.getOne(Id); }
public void insert(CategoriaEntradas categoriaEntradas) { repository.insert(categoriaEntradas); }
public void update(CategoriaEntradas categoriaEntradas){ repository.update(categoriaEntradas);}
public void delete(CategoriaEntradas categoriaEntradas) {repository.delete(categoriaEntradas);}
public void delete(int Id) {repository.delete(Id);}
}
Try this at your Activity or fragment that have your listview.
CategoriaEntradasViewModel mViewModel = ViewModelProviders.of(this).get(CategoriaEntradasViewModel.class);
mViewModel.getAll().observe(this, new Observer<List<CategoriaEntradas>>() {
#Override
public void onChanged(List<CategoriaEntradas> categoriaEntradas) {
adapter = new YourListViewAdapter(this, categoriaEntradas);
listview.setAdapter(adapter);
}
}
Yes, if you have custom list item view. Its similar to array adapter.