Home > OS >  Kotlin Cannot Inflate Layout
Kotlin Cannot Inflate Layout

Time:05-16

I am building an application that has a GridView that contains a dynamic amount of CardViews. I am having issues accessing the TextView elements in the individual items layout from the custom adapter that I am creating.

tvCastleName and tvShieldTime are unresolved references, and the application will not compile.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@ id/tvNickname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-medium"
        android:text="Nickname"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="parent" />

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tvNickname"
        android:horizontalSpacing="6dp"
        android:verticalSpacing="6dp"
        android:numColumns="3"
        android:id="@ id/gvCastles" />

items.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    app:cardElevation="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/tvCastleName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:fontFamily="sans-serif-medium"
            android:text="Castle Name"
            android:gravity="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@ id/tvCastleName"
            android:text="15:00"
            android:id="@ id/tvShieldTime"
            android:gravity="center" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

GridAdapter.kt

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter

class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {


  override fun getCount(): Int = nameList.size

  override fun getItem(position: Int): Castle = nameList[position]

  override fun getItemId(position: Int): Long {
    TODO("Not yet implemented")
  }

  override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val nameList = this.nameList[position]
    var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val castleView = inflater.inflate(R.layout.items, null)
    castleView.tvCastleName.text = nameList.name!!
    castleView.tvShieldTime.text = "15:00"
    
    return castleView
  }

}

CodePudding user response:

You must be getting Unresolved Reference as the method (Kotlin Synthetics) you are using to access view is now deprecated. You can use ViewBinding to access your views.

You've to make a few changes to your getItem method, to make it work with ViewBinding

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val nameList = this.nameList[position]
    val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    itemsBinding.tvCastleName.text = nameList.name!!
    itemsBinding.tvShieldTime.text = "15:00"        
    return itemsBinding.root
}
  • Related