1

In my Room data class I have two constructors. One for excepting a JSON object and a boolean and the other which excepts the JSON object and calls the other with a default boolean.

The problem is that I'm getting the Room: Entities and Pojos must have a usable public constructor error Snd can't seem to se what should be done here:

package com.myapp.android.common.database.room.entities

import android.arch.persistence.room.Entity
import android.arch.persistence.room.Ignore
import android.arch.persistence.room.PrimaryKey
import android.arch.persistence.room.TypeConverters
import com.myapp.android.common.database.room.entities.typeconverters.UserStatusConverter
import org.json.JSONObject

@Entity
@TypeConverters(UserStatusConverter::class)
data class User(
        @PrimaryKey
        var id: Long,
        var name: String,
        var pictureUrl: String,
        var country: String,
        var isPremium: Boolean,
        var mutualFriends: Int?,
        var status: Status?,
        @Ignore
        var isSearch: Boolean
) {    
    constructor(o: JSONObject) :
            this(o, false)

    constructor(o: JSONObject, isSearch: Boolean) :
            this(
                    (if (o.has("id")) o.getLong("id") else -1),
                    (if (o.has("name")) o.getString("name") else "-"),
//                    (if (o.has("email")) o.getString("email") else "-"),
                    (if (o.has("picture_url")) o.getString("picture_url") else ""),
                    (if (o.has("country")) o.getString("country") else ""),
                    (o.optBoolean("is_premium", false) || o.optBoolean("premium", false)),
                    (if (o.has("mutual_friends")) o.getInt("mutual_friends") else -1),
                    (if (o.has("status")) User.Status.toStatus(o.getString("status")) else Status.friend),
                    isSearch
            )

    enum class Status private constructor(val status: String) {
        friend("friends"),
        pending("pending_invite"),
        nofriend("not_friends");

        companion object {
            fun toStatus(status: String): Status? {
                for (type in Status.values()) {
                    if (type.status == status) {
                        return type
                    }
                }
                return null
            }
        }
    }
}
Ambran
  • 2,367
  • 4
  • 31
  • 46
  • Possible duplicate of [Room Persistence: Error:Entities and Pojos must have a usable public constructor](https://stackoverflow.com/questions/44485631/room-persistence-errorentities-and-pojos-must-have-a-usable-public-constructor) – Benjamin Jul 13 '18 at 12:44

0 Answers0