У меня проблема, я получаю данные из базы данных и мне нужно сохранить данные в sharedpreferences
, но при этом выдается следующая ошибка: java.util.HashMap
не может быть приведен к java.util.Collection
Код для моего запроса:
val training2 = mutableMapOf<WeekDay, ArrayList<ArrayList<ExerciseModel>>>()
val train = data["training2"] as MutableMap<*, *>
with(train[WeekDay.SUNDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.SUNDAY] = it
}
}
with(train[WeekDay.MONDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.MONDAY] = it
}
}
, и если я пытаюсь сохранить на sharedPreferences его сбой, мое приложение
fun putUser(context: Context, user: User) {
val sharedPreferences = getSharedPreferences(context)
sharedPreferences.edit().putString("user", Gson().toJson(user)).apply()
}
StackTrace:
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.Collection
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.Gson.toJson(Gson.java:704)
at com.google.gson.Gson.toJson(Gson.java:683)
at com.google.gson.Gson.toJson(Gson.java:638)
at com.google.gson.Gson.toJson(Gson.java:618)
at com.motivapp10.network.service.PreferencesService.putUser(PreferencesService.kt:35)
at com.motivapp10.ui.vc.LoginActivity$onCreate$1$3$1$1$2.invoke(LoginActivity.kt:68)
at com.motivapp10.ui.vc.LoginActivity$onCreate$1$3$1$1$2.invoke(LoginActivity.kt:17)
at com.motivapp10.network.service.UserService$getUserByEmail$1.onComplete(UserService.kt:48)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Класс пользователя:
class User : Serializable {
lateinit var id: String
lateinit var name: String
lateinit var surname: String
lateinit var email: String
lateinit var password: String
var birthDate: Date? = null
lateinit var country: String
var weight: Double = 0.0
var height: Double = 0.0
lateinit var gender: Gender
lateinit var bodyGoal: BodyGoal
var trainingTime: String = "07:00"
val trainingFrequency = mutableMapOf<WeekDay, ExerciseType?>()
var training2 = mutableMapOf<WeekDay, ArrayList<ArrayList<ExerciseModel>>>()
var photo: String? = null
lateinit var dateAdded: Date
var premium: Boolean = false
var expectedTrainings: Long = 0
var doneTrainings: Long = 0
var progress: Double = 0.0
var performance: Double = 0.0
var lastTraining: Training? = null
fun getAge(): Int {
val a = GregorianCalendar()
a.time = birthDate
val b = GregorianCalendar()
b.time = Date()
var diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR)
if (a.get(Calendar.MONTH) > b.get(Calendar.MONTH) || a.get(Calendar.MONTH) == b.get(Calendar.MONTH) && a.get(Calendar.DATE) > b.get(Calendar.DATE)) {
diff--
}
return diff
}
fun load(data: MutableMap<String, Any>) {
name = data["name"] as String
surname = data["surname"] as String
email = data["email"] as String
birthDate = timeStampToDate(data["birthDate"] as Timestamp) as Date
country = data["country"] as String
weight = data["weight"] as Double
height = data["height"] as Double
gender = Gender.valueOf(data["gender"] as String)
bodyGoal = BodyGoal.valueOf(data["bodyGoal"] as String)
trainingTime = data["trainingTime"] as? String ?: "07:00"
val trainingFrequencyData = data["trainingFrequency"] as MutableMap<*, *>
with(trainingFrequencyData[WeekDay.MONDAY.name] as? String) {
trainingFrequency[WeekDay.MONDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.TUESDAY.name] as? String) {
trainingFrequency[WeekDay.TUESDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.WEDNESDAY.name] as? String) {
trainingFrequency[WeekDay.WEDNESDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.THURSDAY.name] as? String) {
trainingFrequency[WeekDay.THURSDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.FRIDAY.name] as? String) {
trainingFrequency[WeekDay.FRIDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.SATURDAY.name] as? String) {
trainingFrequency[WeekDay.SATURDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
with(trainingFrequencyData[WeekDay.SUNDAY.name] as? String) {
trainingFrequency[WeekDay.SUNDAY] = if (this != null) ExerciseType.valueOf(this) else null
}
val train = data["training2"] as MutableMap<*, *>
with(train[WeekDay.SUNDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.SUNDAY] = it
}
}
with(train[WeekDay.MONDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.MONDAY] = it
}
}
with(train[WeekDay.TUESDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.TUESDAY] = it
}
}
with(train[WeekDay.WEDNESDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.WEDNESDAY] = it
}
}
with(train[WeekDay.THURSDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.THURSDAY] = it
}
}
with(train[WeekDay.FRIDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.FRIDAY] = it
}
}
with(train[WeekDay.SATURDAY.name] as? ArrayList<ArrayList<ExerciseModel>>){
this?.let {
training2[WeekDay.SATURDAY] = it
}
}
photo = data["photo"] as? String
dateAdded = timeStampToDate(data["dateAdded"] as Timestamp) as Date
premium = data["premium"] as? Boolean ?: false
}
fun getData(): MutableMap<String, Any> {
val data = mutableMapOf<String, Any>()
data["name"] = name
data["surname"] = surname
data["email"] = email
data["birthDate"] = birthDate!!
data["country"] = country
data["weight"] = weight
data["height"] = height
data["gender"] = gender.name
data["bodyGoal"] = bodyGoal.name
data["trainingTime"] = trainingTime
val trainingFrequencyData = mutableMapOf<String, Any?>()
trainingFrequencyData[WeekDay.MONDAY.name] = trainingFrequency[WeekDay.MONDAY]?.name
trainingFrequencyData[WeekDay.TUESDAY.name] = trainingFrequency[WeekDay.TUESDAY]?.name
trainingFrequencyData[WeekDay.WEDNESDAY.name] = trainingFrequency[WeekDay.WEDNESDAY]?.name
trainingFrequencyData[WeekDay.THURSDAY.name] = trainingFrequency[WeekDay.THURSDAY]?.name
trainingFrequencyData[WeekDay.FRIDAY.name] = trainingFrequency[WeekDay.FRIDAY]?.name
trainingFrequencyData[WeekDay.SATURDAY.name] = trainingFrequency[WeekDay.SATURDAY]?.name
trainingFrequencyData[WeekDay.SUNDAY.name] = trainingFrequency[WeekDay.SUNDAY]?.name
data["trainingFrequency"] = trainingFrequencyData
val trainingData = mutableMapOf<String, Any?>()
trainingData[WeekDay.MONDAY.name] = training2[WeekDay.MONDAY]?.get(0)
trainingData[WeekDay.TUESDAY.name] = training2[WeekDay.TUESDAY]?.get(0)
trainingData[WeekDay.WEDNESDAY.name] = training2[WeekDay.WEDNESDAY]?.get(0)
trainingData[WeekDay.THURSDAY.name] = training2[WeekDay.THURSDAY]?.get(0)
trainingData[WeekDay.FRIDAY.name] = training2[WeekDay.FRIDAY]?.get(0)
trainingData[WeekDay.SATURDAY.name] = training2[WeekDay.SATURDAY]?.get(0)
trainingData[WeekDay.SUNDAY.name] = training2[WeekDay.SUNDAY]?.get(0)
data["training2"] = trainingData
if (photo != null) {
data["photo"] = photo!!
}
data["dateAdded"] = dateAdded
data["premium"] = premium
return data
}
}