Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you add List<MyClass>example to README? #2

Closed
NunciosChums opened this issue Nov 7, 2017 · 10 comments
Closed

Can you add List<MyClass>example to README? #2

NunciosChums opened this issue Nov 7, 2017 · 10 comments

Comments

@NunciosChums
Copy link

I'm using Android Studio 3.0 + kotlin.

// build.gradle
compile "com.marcinmoskala.PreferenceHolder:preferenceholder:1.5"
compile "com.marcinmoskala.PreferenceHolder:preferenceholder-gson-serializer:1.5"
// class App : Application()
override fun onCreate() {
  super.onCreate()
  PreferenceHolder.setContext(applicationContext)
  PreferenceHolder.serializer = GsonSerializer(Gson())
}
class User(name: String, age: Int = 0) {
  var name: String? = name
  var age: Int? = age
}
object AppPreference : PreferenceHolder() {
  var users: List<User>? by bindToPreferenceFieldNullable()
}
// add 
val users: ArrayList<User> = ArrayList()
for (i in (0..5)) {
  users.add(User("name" + i, i))
}
AppPreference.users = users
// get
Log.i("###", AppPreference.users.toString()) 
// => [{age=0.0, name=name0}, {age=1.0, name=name1}, {age=2.0, name=name2}, {age=3.0, name=name3}, {age=4.0, name=name4}, {age=5.0, name=name5}]

for (user in AppPreference.users!!) {
  Log.i("@@@", user.name)
}
// => java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to kr.susemi99.testkotlin.model.User

val users = Gson().fromJson<List<User>>(AppPreference.users)
val users: List<User> = Gson().fromJson(AppPreference.users)
// => compile error

val usersType = object : TypeToken<List<User>>() {}.type
val users = Gson().fromJson<List<User>>(AppPreference.users, usersType)
// => compile error

Please add some more detailed instructions for beginners.

@MarcinMoskala
Copy link
Owner

MarcinMoskala commented Nov 7, 2017

I am checking this out, but my first comment, as a Kotlin teacher, is that you can optimize User and users creation:

class User(var name: String, var age: Int = 0)
val users = List(6) { i -> User("Name$i", i) }

@MarcinMoskala
Copy link
Owner

I cannot reproduce your problem. I've implemented tests for this case.

[{"age":0,"name":"Name0"},{"age":1,"name":"Name1"},{"age":2,"name":"Name2"},{"age":3,"name":"Name3"},{"age":4,"name":"Name4"},{"age":5,"name":"Name5"}]

Try to make two mentioned by my changes.

If you will still have this problem then place, give me more details. First of all, presented User class does not have a toString method implemented, so:

users.toString()

should produce something like this:

[com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@495824e, com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@7b06f6f, com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@6ed747c, com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@809105, com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@1cb755a, com.marcinmoskala.kotlinpreferences.issue2test.Issue2Test$User@efe938b]

Simple Kotlin solution is to use data modifier:

data class User(var name: String, var age: Int = 0)

Also Gson funcions are not working in my project. What Gson version are you using?

@NunciosChums
Copy link
Author

NunciosChums commented Nov 7, 2017

I found something.

I changed User class like this

data class User(var name: String, var age: Int = 0)

and add

val users = List(5) { i -> User("Name$i", i) }
AppPreference.users = users
Log.i("###", "" + AppPreference.users)
// => [User(name=Name0, age=0), User(name=Name1, age=1), User(name=Name2, age=2), User(name=Name3, age=3), User(name=Name4, age=4)]

but after app restart

Log.i("###", "" + AppPreference.users)
// => [{age=0.0, name=Name0}, {age=1.0, name=Name1}, {age=2.0, name=Name2}, {age=3.0, name=Name3}, {age=4.0, name=Name4}]

This is my build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.marcinmoskala.PreferenceHolder:preferenceholder:1.5'
    implementation 'com.marcinmoskala.PreferenceHolder:preferenceholder-gson-serializer:1.5'
}

@MarcinMoskala
Copy link
Owner

This is quite wired. I would expect from toString something like this:
[User(name=Name0, age=0), ...]
And from GSON serializar:
[{"name"="Name0", "age"="0"}, ...]
Your string is neather of them.

Are you sure that you are saving the value before obteining it? Because otherwice you may still operate on some old data that were saved before. If you are not sure then please change AppPreference user property name or set come custom key.

@NunciosChums
Copy link
Author

https://github.com/susemi99/TestKotlinPreference
here is my sample code.

@MarcinMoskala
Copy link
Owner

Your project is working perfectly fine in my env.
workspace 1_091

@NunciosChums
Copy link
Author

Please watch this video.
https://youtu.be/l1I0jeNaVkw

@MarcinMoskala
Copy link
Owner

I am really sorry, you are right. I improved test so it is now checking presented case. I also fixed this problem in version 1.51

@MarcinMoskala
Copy link
Owner

And thank you for your contribution :)

@NunciosChums
Copy link
Author

Thank you very much 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants