Android - How to access Room database from widget

Amr Ahmed

i wanted to access Room DB inside my widget class to update the widget according to the stored data.

the problem here is the ViewModelProvider require a fragment or activity to work with..

ViewModelProviders.of(fragment).get(YourViewModel.class);

which is not available in the app widget class so how to do it?

Aaron Dunigan AtLee

If you use a RemoteViewsService to populate your widget UI, then you can avoid using an AsyncTask by including a DAO query in its onDataSetChanged() method, rather than in the AppWidgetProvider's onUpdate() method. As stated in the onDataSetChanged() documentation,

...expensive tasks can be safely performed synchronously within this method. In the interim, the old data will be displayed within the widget.

The other advantage here is that you can include a call to AppWidgetManager.notifyAppWidgetViewDataChanged() in your app's ViewModel observer's onChanged() method; this will trigger the onDataSetChanged() and the widget will update each time the Room database is changed, rather than just at the fixed interval for onUpdate().

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Android BroadcastReceiver Database Access with Room

分類Dev

How to access QstatusBar in QMainWindows from a widget class?

分類Dev

How to access Stateful widget animation controller from another widget in Flutter?

分類Dev

How can I use GROUPBY date with Android Room Database?

分類Dev

Terminate Room database procedure android

分類Dev

Android room failed to open database

分類Dev

How to access to widget class from another dialog class

分類Dev

Migrate from greendao to Android Room

分類Dev

Android: await() seems not to work using Room database

分類Dev

Items not showing up in RecycleView from Room database

分類Dev

Room database, SELECT * FROM table by a given day

分類Dev

Room Database Query from background Thread

分類Dev

How to sort LiveData from Room Database? Doing a button to switch recyclerview's item's order

分類Dev

How to compare Edit Text with Room Database Text

分類Dev

How to connect to Odoo database from an android application

分類Dev

How to access standard input from android app?

分類Dev

Android room - id of insterted row - from ViewModel

分類Dev

How to Save a number from Access database using Parameter?

分類Dev

How to access a database from a specific server using Java?

分類Dev

Android Room: How to combine data from multiple SQL queries into one ViewModel

分類Dev

Kivy: Access configuration values from any widget

分類Dev

Flutter and Firestore: How can access variables from Stateful Widget to create a new document?

分類Dev

Android: Getting the count of items with a specific code in a Room database

分類Dev

Android Room Database: Difference between OnConflictStrategy.ABORT vs IGNORE

分類Dev

How to Embedd List of Entites in android room?

分類Dev

How to use getSystemService in widget on android?

分類Dev

How can I change the default location of a Room database?

分類Dev

How to copy database from assets folder in android using kotlin

分類Dev

Android How to sort data that i retrieve from Database

Related 関連記事

  1. 1

    Android BroadcastReceiver Database Access with Room

  2. 2

    How to access QstatusBar in QMainWindows from a widget class?

  3. 3

    How to access Stateful widget animation controller from another widget in Flutter?

  4. 4

    How can I use GROUPBY date with Android Room Database?

  5. 5

    Terminate Room database procedure android

  6. 6

    Android room failed to open database

  7. 7

    How to access to widget class from another dialog class

  8. 8

    Migrate from greendao to Android Room

  9. 9

    Android: await() seems not to work using Room database

  10. 10

    Items not showing up in RecycleView from Room database

  11. 11

    Room database, SELECT * FROM table by a given day

  12. 12

    Room Database Query from background Thread

  13. 13

    How to sort LiveData from Room Database? Doing a button to switch recyclerview's item's order

  14. 14

    How to compare Edit Text with Room Database Text

  15. 15

    How to connect to Odoo database from an android application

  16. 16

    How to access standard input from android app?

  17. 17

    Android room - id of insterted row - from ViewModel

  18. 18

    How to Save a number from Access database using Parameter?

  19. 19

    How to access a database from a specific server using Java?

  20. 20

    Android Room: How to combine data from multiple SQL queries into one ViewModel

  21. 21

    Kivy: Access configuration values from any widget

  22. 22

    Flutter and Firestore: How can access variables from Stateful Widget to create a new document?

  23. 23

    Android: Getting the count of items with a specific code in a Room database

  24. 24

    Android Room Database: Difference between OnConflictStrategy.ABORT vs IGNORE

  25. 25

    How to Embedd List of Entites in android room?

  26. 26

    How to use getSystemService in widget on android?

  27. 27

    How can I change the default location of a Room database?

  28. 28

    How to copy database from assets folder in android using kotlin

  29. 29

    Android How to sort data that i retrieve from Database

ホットタグ

アーカイブ