N
N
Newbie Ivanovich2020-04-29 13:46:44
Android
Newbie Ivanovich, 2020-04-29 13:46:44

How to import custom class in kotlin?

there is a Drink class

package com.example.starbuuz
class Drink(name:String, description:String, imageResourceId: Int) {
    private var name:String = name
    private var description:String = description
    private var imageResourceId: Int = imageResourceId
    public var drinks:Array<Drink> = arrayOf(
        Drink("Latte", "A coupleof espresso shots with steamed milk", R.drawable.latte),
        Drink("Cappuccino", "Espresso, hot milk, and a steamed milk foam", R.drawable.cappuccino),
        Drink("Filter","Heighest quality beans roasted and brewed fresh",R.drawable.filter)
        )
    public val getName:String
    get() = name
    public val getDescription:String
    get() = description
    public val getImageResourceId:Int
    get() = imageResourceId
}

which I want to import into the class DrinkCategoryActivity
package com.example.starbuuz

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import com.example.starbuuz.Drink
class DrinkCategoryActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_drink_category)
        val listArrayAdapter: ArrayAdapter<Drink> = ArrayAdapter<Drink>(this,
            android.R.layout.simple_list_item_1,
            Drink.drinks
        )
    }
}

when accessing the Drink.drinks class variable, the word drinks
is underlined in red. This is the code from the book on Head_First._Programmirovanie_dlya_Android page 309
result
spoiler

5ea96d7feb077520830541.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-04-29
@NovichokIvanovich

At the very least, you don't have an instance of the Drink class, you're trying to work with the drinks field, as you would with a static field in Java. There are no statics in Kotlin. Details for example here: https://habr.com/ru/company/funcorp/blog/430836/
And yet, it seems to me that this code will gobble up all the memory and freeze. Because when the first Drink is created, Drink objects will start to be created recursively. Because when each one is created, three more are created, each of which will create three, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question