android-room

Room SQL: Query object with relation 1-to-Many using WHERE parameters on both tables

笑着哭i 提交于 2021-01-29 12:24:26
问题 I have these classes: @Entity public class Person { long id; String name; } @Entity public class Dog { long id; String color; long idPerson; } public class PersonWithDog { @Embedded Person person; @Relation(parentColumn = "id", entityColumn = "idPerson", entity = Dog.class) List<Dog> dogs; } I want to make a query to return a person and a list of only black dogs he owns. Something like: SELECT * FROM Person LEFT JOIN Dogs ON Person.id = Dogs.idPerson WHERE Person.id = ? AND Dogs.color = black

Database Dependent (Room) Conditional startDestination Android Navigation

我的未来我决定 提交于 2021-01-29 11:23:18
问题 I'm setting a conditional startDestination, as e.g. seen here: https://stackoverflow.com/a/51961843/393607 The problem: The condition should depend on a room query. This query can't be run on the main thread of course, the onCreate of the MainActivity would have to wait for the query to be finished before constructing the navigation graph. Any recommendations on how to go about this? 回答1: Ok that was fast. Kotlin's really great: wrapping everything in runBlocking {} did the trick. I'll leave

How to check if room database is empty or not

孤者浪人 提交于 2021-01-29 11:10:37
问题 I am trying to check if room database is empty or not before making a network call. but it is showing below error. error: Not sure how to convert a Cursor to this method's return type (java.lang.Integer). What I have been doing is when app starts I am checking row count in room databse if it is null then I am making a network call. Below is my code. UserDao.java @Dao public interface UserDao { @Query("SELECT * FROM Users") LiveData<Integer> isDbEmpty(); } UserRepository.java public class

How to read DataBaseRoom and display a textView

陌路散爱 提交于 2021-01-29 06:49:21
问题 I am practicing with dataBaseRoom, for them I have created a button, which allow me to read database , i want to put the name of a user and when pressing the button (read data base) reflects me, his name, last name and age in the texview,the problem is that I do not know how to display the data in a textView. Activity where I want to carry out the actions //Entity @Entity(tableName = "user_table") data class User( @PrimaryKey(autoGenerate = true)@ColumnInfo(name = "id") val id: Int, val

How to apply Room TypeConverter to a single field of an entity?

青春壹個敷衍的年華 提交于 2021-01-28 19:07:31
问题 I have been trying different solutions for applying a TypeConverter to a single field of a Room database entity but I am getting an error Cannot figure out how to save this field into database. You can consider adding a type converter for it. When I apply the converter to the entity like this: @Entity(tableName = DBKey.calendarDayTable) @TypeConverters(DateStringConverter::class) data class CalendarDay( @PrimaryKey val date: Date ) everything works as expected, but when I apply it to the

Data Migration after updating Android Ionic App to an Android Native version

时光总嘲笑我的痴心妄想 提交于 2021-01-28 14:25:10
问题 After I reached the limits of Ionic I boldly decided to detach from the web world and learn Kotlin to rewrite the app in the native language. After an amusing challenge I have my new app up and running. But I forgot about one of the most important details : Data Migration . The Ionic version stores data localy in an SQLite database: I am using this Ionic Native Plugin Cordova-sqlite-storage and I nearly do the same thing as this guy over here the only major diffrence I am on Capacitor. In the

Android Room - How to Add a TypeConverter

谁说我不能喝 提交于 2021-01-28 12:14:28
问题 I'm currently learning about the Room Persistence and I just want to ask on how to create a TypeConverter for a custom class? Brand.kt @Parcelize @Entity(tableName = "brand") data class Brand ( @PrimaryKey(autoGenerate = false) @ColumnInfo(name = "id") val id: Int, @ColumnInfo(name = "name") val name: String ) : Parcelable Product.kt @Parcelize @Entity(tableName = "product") data class Product( @PrimaryKey(autoGenerate = false) @ColumnInfo(name = "id") val id: Int, @ColumnInfo(name = "brand")

How to get multiple counts from room database?

大憨熊 提交于 2021-01-28 09:00:56
问题 I want to return multiple counts from Room Select query in android . My query is like Select Count(CASE WHEN x = '0' or x = '2'), Count(Case when a = '33' or a = '23') FROM my_table WHERE id=10 I want above query to return something as list which will contain values of both the above Count() function. This can be easily done using SQLite but I want to use it in room . 回答1: You can give names to the counts and return them as a class, like: data class MyCounts( @ColumnInfo(name = "first_count")

abstract method used without implementation

[亡魂溺海] 提交于 2021-01-28 08:36:41
问题 I was learning the Room Database for Android app development through a tutorial and came across an abstract method being used without its implementation. Following are the files: AppDatabase.java @Database(entities = {Contact.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { private static final String DATABASE_NAME = "database.db"; private static AppDatabase database; **public abstract ContactDao getContactDao();** public static AppDatabase getInstance(Context

Android Room - Sum up values and put it in TextView

故事扮演 提交于 2021-01-28 06:52:27
问题 Maybe this is a tiny bit stupid to ask but I am just stuck with this: I have my Room Database + Entities + Daos. The entities have a column called "costs" - so basically I wanna sum up all the costs (of course while tracking if there was a new input, so check if the sum has changed - if so, of course change the value/textview) and then simply put the value with .toString() into a TextView. I already have a RecyclerView+CardView with an Adapter / ViewModel / Repository Class (they put only the