Clash Royale CLAN TAG#URR8PPP
Query in Room Database in Android Studio
I have a list of notes in room database. I want to choose the list of specific notes depending on some characteristic. For this propose I add a second field note_second_id to a PrimaryKey:
@PrimaryKey(autoGenerate = true)
private long note_id;
private long note_second_id;
Then I create the next query:
@Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE + " WHERE note_second_id = :second_id ")
List<Note> getNotes(long second_id);
When I want to add a new note I use:
long j = activityReference.get().dataBase.getNoteDao().insertNote(note);
deck.setNote_id(j);
deck.setNote_second_id(note000);
where methods .setNote_id(), and setNote_second_id() look like:
public void setNote_id(long deck_id) {this.note_id = note_id; }
public void setNote_second_id(long notesecondid) {
this.note_second_id = notesecondid; }
and note000 is a constant:
Long note000 = 11111L;
And in the activity, where I want these list to be shown I write:
private class RetrieveTaskDeck extends AsyncTask<Void,Void,List<Deck>> {
private WeakReference<MainActivity> activityReference;
// only retain a weak reference to the activity
RetrieveTaskDeck(MainActivity context) {
activityReference = new WeakReference<>(context); }
@Override
protected List<Note> doInBackground(Void... voids) {
if (activityReference.get()!=null)
return activityReference.get().dataBase.getNotesDao().getNotes(note000);
else
return null; }
@Override
protected void onPostExecute(List<Note> notes) {
if (notes !=null && notes.size()>0 ){
activityReference.get().notes.clear();
activityReference.get().notes.addAll(notes);
// hides empty text view
activityReference.get().textViewMsgDeck.setVisibility(View.GONE);
activityReference.get().decksAdapter.notifyDataSetChanged(); } } }
The problem is that this way does not work. It does not show any of the notes. Only when I use the query:
@Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE)
List<Note> getAllNotes();
and
activityReference.get().dataBase.getNotesDao().getAllNotes();
only then I get the list of all notes, which I have. But I need only some specially marked notes to be shown.
What is wrong and what can I do? It seems like something is wrong with query... Maybe someone, who had an experience working with room databes and queries for it, may give some advice...
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.