0

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...

1 Answer 1

0

Actually, I have solved the problem. Here is a solution. I used the next query:

@Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE + " where note_content LIKE :second_id ")
List<Note> getNotes(String second_id);

Where "note_content" is a name of a column and "second_id" is a first row, written after the column name:

@ColumnInfo(name = "note_content")
private String second_id; 

Pay attention, if I write String second_id somewhere after the first String value (second or third) after the proclamation of the column, it does not make search by this word. But in this way, written above, it works.

So, I can set this String value "second_id" of the column dynamically, depending on the task, and a user does not need to know that it even exists. I tried playing with ID in PrimaryKey to solve the same task, but it didn't work. Actually, using of the String value in Column works. Hope it will help someone. Also, if anyone knows how to solve the same task using IDs in PrimaryKey, let me know writing your solution here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.