Data Class and properties from other Data Class - android

I'm downloading few lists (RawAlbum, RawPhoto) from the web and I want to use properties from them to create another list. Then I want to use it to make a recyclerview.
So that's how it looks. It is the ListItem:
#Parcelize
data class ListItem(
val id: Int,
val title: String,
val albumTitle: String,
val thumbnailUrl: String
) : Parcelable
So I want to use the id and title from the RawAlbum. While the albumTitle and thumbnailUrl from the RawPhoto.
RawAlbum:
#Parcelize
#Entity(tableName = "albums")
data class RawAlbum(
#PrimaryKey
val id: Int,
val userId: Int,
val title: String
) : Parcelable
RawPhoto:
#Parcelize
#Entity(tableName = "photos")
data class RawPhoto(
#PrimaryKey
val id: Int,
val albumId: Int,
val title: String,
val url: String,
val thumbnailUrl: String
) : Parcelable

Related

Can't parse json correctly

need help in parsing this json file from this endpoint
http://ergast.com/api/f1/constructors
this is my data class
data class Teams(
val MRData: MRdata
){
data class MRdata(
val ConstructorTable: ConstructorsTable,
val limit: String,
val offset: String,
val series: String,
val total: String,
val url: String,
val xmlns: String
){
data class ConstructorsTable(
val Constructors: List<Constructor>?
) {
data class Constructor(
val constructorId: String?,
val name: String?,
val nationality: String?,
val url: String?
)
}
}
}
when i use Teams class as a return model it logs actual data but, when i try to return specific data like constructorID or nationality it returns null.

Parameter specified as non-null is null error in Room Database

Here is my entity class:
#Entity
data class User(
#PrimaryKey
#Json(name = "id") val userId: String,
#Json(name = "login") val userName: String,
#Json(name = "avatar_url") val userAvatar: String,
val profile: Profile? = null
) : Serializable
Here is my Profile data class
data class Profile(
val avatar_url: String,
val bio: String,
val blog: String,
val company: Any,
val created_at: String,
val email: Any,
val events_url: String,
val followers: Int,
val followers_url: String,
val following: Int,
val following_url: String,
val gists_url: String,
val gravatar_id: String,
val hireable: Boolean,
val html_url: String,
val id: Int,
val location: String,
val login: String,
val name: String,
val node_id: String,
val organizations_url: String,
val public_gists: Int,
val public_repos: Int,
val received_events_url: String,
val repos_url: String,
val site_admin: Boolean,
val starred_url: String,
val subscriptions_url: String,
val twitter_username: Any,
val type: String,
val updated_at: String,
val url: String
) : Serializable
but every time I try to insert data into the table I am getting the error, how can I insert null data object in table while using room database?
The issue here is that Room doesn't know how to insert attribute of type Profile to the table.
The simple solution would be to use a type converter. Something like the following:
class DatabaseConverters {
#TypeConverter
fun toProfile(profileJson: String): Profile? {
return <Create a Profile object out of a JSON string>
}
#TypeConverter
fun fromProfile(profile: Profile?): String {
return <JSON string representation of Profile object>
}
}
In your case - you can use "" (empty string) when Profile is null.
More info about converters: Here

Android Room: Entities and POJOs must have a usable public constructor

I'm trying to save the data class i got from json to room database and suddenly get errors like this while building the app.
Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
NewsResponse model object
data class NewsResponse(
val copyright: String,
val response: Response,
val status: String
)
Response object
data class Response(
val docs: List<Docs>
)
Docs object(this is the data class i want to save)
#Entity(
tableName = "docs"
)
data class Docs(
#PrimaryKey(autoGenerate = true)
var id: Int? = null,
val _id: String,
val `abstract`: String,
#Embedded
val byline: Byline,
val document_type: String,
#Embedded
val multimedia: Multimedia,
#Embedded
val headline: Headline,
val lead_paragraph: String,
val news_desk: String,
val pub_date: String,
val section_name: String,
val snippet: String,
val source: String,
val subsection_name: String,
val type_of_material: String,
val uri: String,
val web_url: String,
val word_count: Int
)
Byline object
#Entity
data class Byline(
val original: String
)
Multimedia object
#Entity
data class Multimedia(
val url: String
)
Headline object
#Entity
data class Headline(
val main: String
)
Any ideas how to solve this?

Insert Nested data classes into Room db

I'm in situation where i have other model classes nested inside a parent class , and the thing is that i want to insert the model classes into room db but it is asking me to use type converter but i don't know to and from which type should i convert the data class , this is my code
data class FixturesMergedModels (
#Embedded var event: Event,
#Embedded var teamXX: TeamXX,
#Embedded var awaylogo: testAwayLogoModel
)
My Event Model
#Entity
data class Event(
#PrimaryKey(autoGenerate = true)
var evenid : Int?,
#SerializedName("dateEvent")
var dateEvent : String?,
#SerializedName("strTime")
val strTime : String?,
#SerializedName("strThumb")
val strThumb : String?,
#SerializedName("strHomeTeam") //
val strHomeTeam : String?,
#SerializedName("strAwayTeam") //
val strAwayTeam : String?,
#SerializedName("idAwayTeam")
val idAwayTeam: Int?,
#SerializedName("idEvent")
val idEvent: Int?,
#SerializedName("idHomeTeam")
val idHomeTeam : Int?,
#SerializedName("intHomeScore") //
val intHomeScore : String?,
#SerializedName("intAwayScore") //
val intAwayScore : String?
)```
* Home Team Model ( teamxx)
#Entity
data class TeamXX(
#PrimaryKey(autoGenerate = true)
var homeTeamID : Int?,
#SerializedName("strTeamBadge")
val HomestrTeamBadge: String?
)```
Away Team Model
data class testAwayLogoModel (
#PrimaryKey(autoGenerate = true)
var awayTeamID : Int?,
#SerializedName("strTeamBadge")
val AwaystrTeamBadge: String?
)
According to the google developer website, you should use #Embedded for your event and teamXX class. Here is a short example given at their website.
data class Address(
val street: String?,
val state: String?,
val city: String?,
#ColumnInfo(name = "post_code") val postCode: Int)
#Entity
data class User(
#PrimaryKey val id: Int,
val firstName: String?,
#Embedded val address: Address?
)
for more detail you can visit here: https://developer.android.com/training/data-storage/room/relationships

How to get related data using Room

#Entity
data class User(
#PrimaryKey
val id: Int,
val petsId: List<String>
)
#Entity
data class Pet(
#PrimaryKey
val id: Int,
val name: String
)
How to get UserWithPets from the two User and Pet entities using Room?
data class UserWithPets(
val id: Int,
val pets: List<Pet>
)

Categories

Resources