Why can't I access my ListView in the MainActivity file?
I created a ListView in my activity_main.xml file like this:
<?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"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="15dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm trying to populate it like so in my MainActivity.kt file:
var adapter = ArrayAdapter<String>(applicationContext,android.R.layout.simple_dropdown_item_1line,post)
listview.adapter = adapter
However, I get an error on listview and it says that it has an Unresolved reference. I am following a tutorial and they didn't have this issue. It might just be that the tutorial is a bit date (it was made in April 2020) and accessing layout variables has changed.
Any suggestions?
Posted Mar 21, 2021, 9:47 PM UTC
23
That seems to be using Kotlin synthetics, which is not supported anymore. The recommended way now is using View Binding. Have a read at this: https://developer.android.com/topic/libraries/view-binding/migration
Thank you