Android: Add inspection & quickfix to convert findViewById to api 26
#KT-19940 Fixed Target Version 1.1.5
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
@Suppress(
|
||||
"TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER",
|
||||
"UNUSED_VARIABLE"
|
||||
)
|
||||
class OtherActivity : Activity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_other)
|
||||
|
||||
<weak_warning descr="Can be converted to findViewById<TextView>(...)">findViewById(R.id.tvHello) as TextView</weak_warning>
|
||||
val tvHello = <weak_warning descr="Can be converted to findViewById<TextView>(...)">findViewById(R.id.tvHello) as TextView</weak_warning>
|
||||
val btnGo = <weak_warning descr="Can be converted to findViewById<Button>(...)">findViewById(R.id.btnGo) as Button?</weak_warning>
|
||||
|
||||
// should be ok, already has type parameter
|
||||
val tvHello2 = findViewById<TextView>(R.id.tvHello) as TextView
|
||||
|
||||
// ok, we can't convert safe cast because semantic will be changed
|
||||
val tvHello3 = findViewById(R.id.tvHello) as? TextView
|
||||
|
||||
// ok, no cast
|
||||
foo(findViewById(R.id.tvHello))
|
||||
|
||||
// ok, no cast
|
||||
findViewById(R.id.tvHello) is TextView
|
||||
}
|
||||
|
||||
fun foo(view: TextView) {
|
||||
view.text = "foo"
|
||||
}
|
||||
}
|
||||
|
||||
class R {
|
||||
object layout {
|
||||
val activity_other = 100500
|
||||
}
|
||||
|
||||
object id {
|
||||
val tvHello = 0
|
||||
val btnGo = 1
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -29,7 +29,7 @@ abstract class ViewHolderTest : BaseAdapter() {
|
||||
// Should use View Holder pattern here
|
||||
convertView = mInflater.<warning descr="Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling">inflate(R.layout.your_layout, null)</warning>
|
||||
|
||||
val text = convertView.findViewById(R.id.text) as TextView
|
||||
val text: TextView = convertView.findViewById(R.id.text)
|
||||
text.text = "Position " + position
|
||||
|
||||
return convertView
|
||||
@@ -46,7 +46,7 @@ abstract class ViewHolderTest : BaseAdapter() {
|
||||
convertView = mInflater.inflate(R.layout.your_layout, null)
|
||||
}
|
||||
|
||||
val text = convertView!!.findViewById(R.id.text) as TextView
|
||||
val text: TextView = convertView!!.findViewById(R.id.text)
|
||||
text.text = "Position " + position
|
||||
|
||||
return convertView
|
||||
@@ -65,7 +65,7 @@ abstract class ViewHolderTest : BaseAdapter() {
|
||||
convertView = mInflater.inflate(R.layout.your_layout, null)
|
||||
}
|
||||
|
||||
val text = convertView!!.findViewById(R.id.text) as TextView
|
||||
val text: TextView = convertView!!.findViewById(R.id.text)
|
||||
text.text = "Position " + position
|
||||
|
||||
return convertView
|
||||
@@ -80,7 +80,7 @@ abstract class ViewHolderTest : BaseAdapter() {
|
||||
// Already using View Holder pattern
|
||||
convertView = if (convertView == null) mInflater.inflate(R.layout.your_layout, null) else convertView
|
||||
|
||||
val text = convertView!!.findViewById(R.id.text) as TextView
|
||||
val text: TextView = convertView!!.findViewById(R.id.text)
|
||||
text.text = "Position " + position
|
||||
|
||||
return convertView
|
||||
@@ -99,16 +99,16 @@ abstract class ViewHolderTest : BaseAdapter() {
|
||||
var v: View? = convertView
|
||||
if (v == null) v = mLayoutInflator!!.inflate(R.layout.your_layout, null)
|
||||
|
||||
val listItemHolder = v!!.findViewById(R.id.laptimes_list_item_holder) as LinearLayout
|
||||
val listItemHolder: LinearLayout = v!!.findViewById(R.id.laptimes_list_item_holder)
|
||||
listItemHolder.removeAllViews()
|
||||
|
||||
for (i in 1..5) {
|
||||
val lapItemView = mLayoutInflator!!.inflate(R.layout.laptime_item, null)
|
||||
val lapItemView: View = mLayoutInflator!!.inflate(R.layout.laptime_item, null)
|
||||
if (i == 0) {
|
||||
val t = lapItemView.findViewById(R.id.laptime_text) as TextView
|
||||
val t: TextView = lapItemView.findViewById(R.id.laptime_text)
|
||||
}
|
||||
|
||||
val t2 = lapItemView.findViewById(R.id.laptime_text2) as TextView
|
||||
val t2: TextView = lapItemView.findViewById(R.id.laptime_text2)
|
||||
if (i < mLapTimes.size - 1 && mLapTimes.size > 1) {
|
||||
var laptime = mLapTimes[i] - mLapTimes[i + 1]
|
||||
if (laptime < 0) laptime = mLapTimes[i]
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// INTENTION_TEXT: Convert cast to findViewById with type parameter
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
|
||||
class OtherActivity : Activity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_other)
|
||||
|
||||
val tvHello = <caret>findViewById(R.id.tvHello) as TextView?
|
||||
}
|
||||
}
|
||||
|
||||
class R {
|
||||
object layout {
|
||||
val activity_other = 100500
|
||||
}
|
||||
|
||||
object id {
|
||||
val tvHello = 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// INTENTION_TEXT: Convert cast to findViewById with type parameter
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
|
||||
class OtherActivity : Activity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_other)
|
||||
|
||||
val tvHello = findViewById<TextView>(R.id.tvHello)
|
||||
}
|
||||
}
|
||||
|
||||
class R {
|
||||
object layout {
|
||||
val activity_other = 100500
|
||||
}
|
||||
|
||||
object id {
|
||||
val tvHello = 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// INTENTION_TEXT: Convert cast to findViewById with type parameter
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
|
||||
class OtherActivity : Activity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_other)
|
||||
|
||||
val tvHello = <caret>findViewById(R.id.tvHello) as TextView
|
||||
}
|
||||
}
|
||||
|
||||
class R {
|
||||
object layout {
|
||||
val activity_other = 100500
|
||||
}
|
||||
|
||||
object id {
|
||||
val tvHello = 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// INTENTION_TEXT: Convert cast to findViewById with type parameter
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.android.inspection.TypeParameterFindViewByIdInspection
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
|
||||
|
||||
class OtherActivity : Activity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_other)
|
||||
|
||||
val tvHello = findViewById<TextView>(R.id.tvHello)
|
||||
}
|
||||
}
|
||||
|
||||
class R {
|
||||
object layout {
|
||||
val activity_other = 100500
|
||||
}
|
||||
|
||||
object id {
|
||||
val tvHello = 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user