2

I am doing a map activity, on marker click, I would display the information on a textview. The information is saved in android room database because I need the data to persist.

I want to query only a row with the name of the column, marker tag

I have searched many ways to query a row, but none of them is specific to my questions.

Peter
  • 437
  • 1
  • 4
  • 20
  • Yes, you can use AsynTask. For simple execution, just use Executor of Room . – sushildlh Apr 25 '19 at 10:14
  • You need to do Room queries on background threads. However, `AsyncTask` is obsolete. Modern Android development uses other approaches for thread management. For example, your Room DAO could have this `@Query` method return `LiveData` or an RxJava type (e.g., `Single`). – CommonsWare Apr 25 '19 at 11:04
  • @CommonsWare, what do u mean? I did have Room DAO with Many atQuery. But there is nothing online to suggest how to properly use it to call only a single row, all the tutorials are for insert/query all/delete all – Peter Apr 25 '19 at 12:16
  • "But there is nothing online to suggest how to properly use it to call only a single row" -- um, that is just SQL (e.g., `SELECT ... FROM table WHERE _ID = :id`). The Room documentation shows single-entity DAO queries (see [here](https://developer.android.com/training/data-storage/room/index.html)). – CommonsWare Apr 25 '19 at 12:47
  • [One of my books](https://commonsware.com/AndroidArch/) covers single-entity DAO queries. Samples include [this](https://github.com/commonsguy/cw-androidarch/blob/FINAL/Trips/RoomBasics/app/src/main/java/com/commonsware/android/room/TripStore.java#L30-L31) and [this](https://github.com/commonsguy/cw-androidarch/blob/FINAL/Trips/RxRoom/app/src/main/java/com/commonsware/android/room/TripStore.java#L36-L37). – CommonsWare Apr 25 '19 at 12:49
  • @CommonsWare sorry, maybe I am not clear. my question is on how to do the repository class without using the asyntask, becuase andorid room's tutorial did not suggest how to do it? – Peter Apr 25 '19 at 13:23
  • As I wrote, your Room DAO could have this `@Query` method return `LiveData` or an RxJava type (e.g., `Single`). [The Room documentation](https://developer.android.com/training/data-storage/room/accessing-data) shows examples of both of these. – CommonsWare Apr 25 '19 at 13:36
  • hi @CommonsWare, firstly thanks for your patience, I am really lost here. my database has a DAO method that returns `LiveData`. But how do I fetch/call/ access it? – Peter Apr 25 '19 at 13:39
  • https://developer.android.com/topic/libraries/architecture/livedata – CommonsWare Apr 25 '19 at 13:57

1 Answers1

5

Yes, reading and writing to Room database must be asynchronous. So you must run your queries in:

AsyncTask.execute(new Runnable()
{
    @Override
    public void run()
    {

        // run your queries here!
    }
});

Good luck.

Arantik
  • 584
  • 8
  • 17