Home > Software design >  ListView var not found
ListView var not found

Time:01-02

I followed a tutorial on how to do a listview in kotlin and I encountered this problem. I'm new to Kotlin and I don't understand what are the getters and setters for my activity. From what I read it should be put automatically.

private lateinit var listView ListView

Property getter or setter expected

All code:

package com.example.librariasemnelor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button
import android.widget.ListView;
private lateinit var listView ListView

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val actionBar = supportActionBar

        if(actionBar != null){
            actionBar.title = "Meniu principal"

            actionBar.setDisplayHomeAsUpEnabled(true)
        }


        listView = findViewById<ListView>(R.id.recipe_list_view)
// 1
        val recipeList = Recipe.getRecipesFromFile("recipes.json", this)
// 2
        val listItems = arrayOfNulls<String>(recipeList.size)
// 3
        for (i in 0 until recipeList.size) {
            val recipe = recipeList[i]
            listItems[i] = recipe.title
        }
// 4
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems)
        listView.adapter = adapter



    }

}

CodePudding user response:

private lateinit ... should be placed inside the class block. And : must be added just after listView.

class SecondActivity : AppCompatActivity() {

    private lateinit var listView: ListView

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
    }

    ...

}
  • Related