-1

I'm beginner in Kotlin for Android. I have some stupid questions which i can't find answers.

First question. For Example i have a code like this

class myAdapter(context: Context, val list: List<DataSource>)
: ArrayAdapter<DataSource>(context, 0, list){

If i write like that (without arguments)

class myAdapter(context: Context, val list: List<DataSource>)
: ArrayAdapter<DataSource>(){

then intellij give me out this error: None of the following functions can be called with the arguments supplied. Why should i to provide arguments to ArrayAdapter and what are they affected?

Second question. For example, i have a code:

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

    val lf: LayoutInflater = LayoutInflater.from(context)
    val row = lf.inflate(R.layout.custom_list_view, parent, false)

    val view_icon = row.findViewById<ImageView>(R.id.icon)
    val view_title = row.findViewById<TextView>(R.id.title)
    val view_subtitle = row.findViewById<TextView>(R.id.subtitle)

    val ds: DataSource = list[position]
    view_icon.setImageResource(R.drawable.ic_folder_black_24dp)
    view_title.text = ds.ds_title
    view_subtitle.text = ds.ds_subtitle

    return row
}

What's means the "row" variable and why should i to assign "inflate" to it? How explain it with words? If i don't assign, then inflate will not work. And what's means this:

list[position]

Where can i read about it? Kotlin just break down my mind.

scrapjack
  • 21
  • 4
  • 2
    Although you're focused on Android development, understanding the Kotlin language will help you tremendously: https://kotlinlang.org/docs/reference/ – Slaw Feb 04 '20 at 17:33

1 Answers1

0

Why should i to provide arguments to ArrayAdapter and what are they affected?

Basically when you have a line like class MyClass(xxx) : SuperClass(yyy) it means you're creating a new class called MyClass with a constructor that takes 1 argument xxx and it extends SuperClass – of which you're calling the constructor with argument yyy.

Now, putting that in the context of your example, you're first trying to call the ArrayAdapter's constructor with 3 arguments – a Context, an int and aList – which exists and can be called. However ArrayAdapter doesn't have any constructor with no arguments, hence the compilation error.

As for the second question, you can read more on the official documentation or here for example – I think you'll find plenty of material online – but basically "inflate" means "render a UI element", so row is the variable that contains a reference to the row you just inflated.

user2340612
  • 10,053
  • 4
  • 41
  • 66