I'm attempting to implement parts of Mike Penz' NavigationDrawer (https://github.com/mikepenz/MaterialDrawer) in Kotlin. Since then I've run into only a few issues, primarily with operators. Here is part of the code to instantiate the drawer itself. Android Studio doesn't throw any errors except where I'm using the == operator on int and Long variables:
// Create the Drawer result = DrawerBuilder() .withSliderBackgroundColor(ContextCompat.getColor(applicationContext, R.color.top_header)) .withActivity(this) .withToolbar(toolbar) .withHasStableIds(true) .withItemAnimator(AlphaCrossFadeAnimator()) .withAccountHeader(headerResult!!) .addDrawerItems( PrimaryDrawerItem().withName(R.string.drawer_item_profile).withIcon(FontAwesome.Icon.faw_user).withIdentifier(1).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)), PrimaryDrawerItem().withName(R.string.drawer_item_create).withIcon(FontAwesome.Icon.faw_paint_brush).withIdentifier(2).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)), PrimaryDrawerItem().withName(R.string.drawer_item_yaanich_news).withIcon(FontAwesome.Icon.faw_newspaper_o).withIdentifier(3).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)), PrimaryDrawerItem().withName(R.string.drawer_item_my_groups).withIcon(FontAwesome.Icon.faw_users).withIdentifier(4).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)), PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(5).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)) ) .withOnDrawerItemClickListener { view, position, drawerItem -> if (drawerItem != null) { var intent: Intent? = null if (drawerItem.identifier == (1) { intent = Intent(this, UserProfileActivity::class.java) } else if (drawerItem.identifier == 2) { intent = Intent(this, YeetActivity::class.java) } else if (drawerItem.identifier == 3) { intent = Intent(this, RssActivity::class.java) } else if (drawerItem.identifier == 4) { intent = Intent(this, GroupsActivity::class.java) } else if (drawerItem.identifier == 5) { intent = Intent(this, UserSettingsActivity::class.java) } if (intent != null) { this.startActivity(intent) } } false } .withSavedInstance(savedInstanceState) .withShowDrawerOnFirstLaunch(true) .build() RecyclerViewCacheUtil<IDrawerItem<*, *>>().withCacheSize(2).apply(result!!.recyclerView, result!!.drawerItems) if (savedInstanceState == null) { result!!.setSelection(21, false) headerResult!!.activeProfile = profile } }
Errors:
if (drawerItem.identifier == (1)
if (drawerItem.identifier == 2)
Operator == cannot be applied to 'Long and' 'Int'
== operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references. The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.
Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.
Long is a 64-bit number in Kotlin The type can be either declared explicitly or the compiler itself has the ability to infer the type of the assigned value. The long data type can have values from -(2^63) to (2^63)-1.
Simply use long on your right side
if (drawerItem.identifier == 1L)
Edit: the reason this is required is that Kotlin does not promote Ints to Longs (or, more generally, does not widen types); on the left side we had a Long and on the right side we had an Int, which lead to the error. Explicitly indicating that the right side is a Long fixes the error.
For interest, another solution would be to use compareTo()
. compareTo returns zero if the values are equal, negative if its less than the other, or positive if its greater than the other:
if(drawerItem.identifier.compareTo(1) == 0) "Equals"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With