https://kotlinlang.org logo
#android
Title
# android
s

Sam

09/16/2019, 6:37 AM
Good morning, everybody, I got warning like this. Why android studio show this warning on these codes? and how to resolve this problem ? Thanks.
t

Tsvetozar Bonev

09/16/2019, 6:39 AM
Press alt+enter and it will suggest an annotation on the line, method, class or file that will ignore the warning. The warning shows up because <*> is not equivalent to <String>
s

Sam

09/16/2019, 6:43 AM
But I hate to add more annotation, its really messy . Anything else suggestion to remove this warning?
t

Tsvetozar Bonev

09/16/2019, 6:43 AM
One annotation on the file level will ignore every warning of this type, it isn't that messy
The warning itself is just a warning, you can ignore it
s

Sam

09/16/2019, 6:45 AM
Thanks @Tsvetozar Bonev
K 1
l

Luca Nicoletti

09/16/2019, 8:01 AM
Well, ignoring the cast might not be the best solution
Also, your check:
is ArrayList<*>
does not assure you you have an
ArrayList<String>
I’d update the check 🙂
s

Sam

09/16/2019, 8:03 AM
Yes, I have a lot of models which different types, so I must use
Any
for flexible to cast Type of data in RecyclerView.
l

Luca Nicoletti

09/16/2019, 8:03 AM
I’d then add a check before casting it to
ArrayList<String>
s

Sam

09/16/2019, 8:04 AM
Screen Shot 2019-09-16 at 3.04.30 PM.png
You mean this things???
l

Luca Nicoletti

09/16/2019, 8:06 AM
Yes, I forgot that you cannot do that 😞 Sorry
😢 1
You could introduce nullability for safety 🙂
as? ArrayList<String>?
that would return
null
if the cast fails
s

Sam

09/16/2019, 8:12 AM
thanks @Luca Nicoletti, its still warning and suggest to
Array<*>
with
as?
l

Luca Nicoletti

09/16/2019, 8:12 AM
I was talking about the second cast
Inside the
if
🙂
👍 1
s

starke

09/16/2019, 5:51 PM
Yes, I have a lot of models which different types, so I must use
Any
for flexible to cast Type of data in RecyclerView.
there are other ways of representing your data where you don’t have to deal with a list of
Any
using something like https://github.com/lisawray/groupie can do a lot of the heavy lifting there for you too
3 Views