Android Kotlin: ‘getter for position int’ is deprecated. deprecated in java


Today I encountered a deprecation warning as I’m coding the app: 'getter for position int' is deprecated. deprecated in java

And I was like, whoa, this wasn’t deprecated a couple of months back when I last touched Android code… this is how long I’ve missed out! 😂😂😂 I haven’t touched any app code for a while, until this week. I am easing back into Android app development, and this time it’s for my personal use but hoping to share on Google Play Store for free… just in case it may help others.

The following RecyclerView Adapter code needs polishing 😂, but for the sake of showing you the deprecation warning, here’s a screenshot:

android getter for position int is deprecated. deprecated in java

The deprecation warning is inside the ViewHolder class inside my RecyclerView Adapter file. By the way, I code in Kotlin now for Android apps.

I worked with a developer before who liked suppressing deprecation errors like this, but I advised him that it’s better to address the issue than suppress it because it will haunt him in the future! 🤣 I still believe in that up to now, so I searched for a solution.

This is the problematic line, and ‘position’ is the problematic variable:

The solution? Simply replace position with adapterPosition.

And why is it deprecated? I found a note in RecyclerView.java:

* @deprecated This method is deprecated because its meaning is ambiguous due to the async
* handling of adapter updates. You should use {@link #getLayoutPosition()} or
* {@link #getAdapterPosition()} depending on your use case.

That’s all I have to say about the ‘getter for position int’ is deprecated. deprecated in java warning. Happy coding, you all.

Catzie

A Filipino programmer and baker. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. When I have the rare luxury of time, I get to travel and record song covers too! Baked-Per-Order Sweets: Catzie's Cakery Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *