0

image of the file

listItemView = inflater.inflate(R.layout.list_item, parent,false)

I am unable to access the layout file from the inflate method

class AdapterEarthquake(var cntx: Context,var earthquake: ArrayList<earthquake>):ArrayAdapter<earthquake>( cntx,0, earthquake){

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

    var listItemView = convertView
    if(listItemView == null) {
        val inflater:LayoutInflater = LayoutInflater.from(cntx)
        listItemView = inflater.inflate(R.layout.list_item, parent,false)
    }

    val currentItem = getItem(position)

    val magTextView = listItemView.findViewById<TextView>(R.id.magnitude_text_view)

    return super.getView(position, convertView, parent)
}

}

magnitude_text_view also shows error,but that's probably because listItemView shows error

The name of the file is saved as list_item

val magTextView = listItemView.findViewById<TextView>(R.id.magnitude_text_view)

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mag"
        style="@style/mag"
        android:id="@+id/magnitude_text_view"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="London"
        android:layout_weight="60"
        android:layout_gravity="center"
        android:textColor="@color/black"
        android:id="@+id/place_text_view"

        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Feb 26, 1998"
        android:layout_weight="40"
        android:layout_gravity="center"
        android:id="@+id/date_text_view"

        />

1 Answers1

0

Add this in your kotlin class file, where you want to inflate the layout.

import kotlinx.android.synthetic.main.activity_main.*

And in the build.gradle (:app) file add as below in plugins

id 'kotlin-android'
id 'kotlin-android-extensions'

Note : Use the element id, not the file itself

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    />