I am using kotlin to create my adapter which extends BaseAdapter in android. below is the code inside getView method
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
var binding : ImageBinding
var conView = convertView
if(conView == null){
binding = DataBindingUtil.inflate(LayoutInflater.from(parent?.context),
R.layout.image, parent, false)
conView = binding.root;
conView.tag = binding
}else {
binding = conView.getTag() as ImageBinding
}
return conView;
}
conView.tag = binding and binding = conView.getTag() is highlighted in a pink-like color.When i hover over conView.tag = binding with my mouse, a popup appears with a message Smart cast to android.view.View!. And when i hover over binding = conView.tag,a popup appears with a message Smart cast to android.view.View. Note the difference in the two messages where the latter is missing the platform type sign (!)
How can i implement the two suggested options?
Because
convertViewis a nullable type variable(View?),var conView = convertViewassigment creates a copy of a nullableconViewvariable. So you should handleconViewvariable as nullable.Try the following way:
Note: Semicolons in Kotlin are not required, be aware of that