Error in Super keyword in ArrayAdapter in kotlin











up vote
-1
down vote

favorite












I was trying to send the word ArrayList in other ArrayDapter class, but the error arises that it has too many arguments and super keyword in kotlin can only be used on the left side of "." operator.



    package com.example.android.miwok

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.ArrayAdapter
import android.widget.ListView


class NumbersActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_numbers)

val words = ArrayList<Word>()
words.add(Word("one","lutti"))
words.add( Word("two", "otiiko"));
words.add( Word("three", "tolookosu"));
words.add( Word("four", "oyyisa"));
words.add( Word("five", "massokka"));
words.add( Word("six", "temmokka"));
words.add( Word("seven", "kenekaku"));
words.add( Word("eight", "kawinta"));
words.add( Word("nine", "wo’e"));
words.add( Word("ten", "na’aacha"));

val adapter = WordAdapter(this,words)

val listView = findViewById(R.id.list) as ListView?

listView!!.setAdapter(adapter)
}
}


The WordAdapter.kt file:-



package com.example.android.miwok

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.app.Activity
import android.content.Context
import android.widget.TextView
import android.view.LayoutInflater


class WordAdapter : ArrayAdapter<Word>() {

fun WordAdapter(context: Activity, words: ArrayList<Word>){
super.ArrayAdapter<Word>(context, 0, words)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Check if the existing view is being reused, otherwise inflate the view
var listItemView = convertView
if (listItemView == null) {
listItemView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false)
}

val currentWord = getItem(position)

val nameTextView = listItemView!!.findViewById(R.id.miwok_text_view) as TextView

nameTextView.setText(currentWord!!.getDefaultTranslation())

val numberTextView = listItemView.findViewById(R.id.default_text_view) as TextView

numberTextView.setText(currentWord!!.getMiwokTranslation())

return listItemView
}
}




I am not able to find the error in this.



The word super should be linked to which part is also confusable to me.










share|improve this question




















  • 1




    Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
    – Zoe
    yesterday










  • @Zoe, he wants to show more than one view per row.
    – Andre Artus
    yesterday










  • @AndreArtus so? Just initializing as ArrayAdapter is more than enough
    – Zoe
    yesterday










  • @Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
    – Andre Artus
    yesterday










  • The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
    – Zoe
    yesterday

















up vote
-1
down vote

favorite












I was trying to send the word ArrayList in other ArrayDapter class, but the error arises that it has too many arguments and super keyword in kotlin can only be used on the left side of "." operator.



    package com.example.android.miwok

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.ArrayAdapter
import android.widget.ListView


class NumbersActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_numbers)

val words = ArrayList<Word>()
words.add(Word("one","lutti"))
words.add( Word("two", "otiiko"));
words.add( Word("three", "tolookosu"));
words.add( Word("four", "oyyisa"));
words.add( Word("five", "massokka"));
words.add( Word("six", "temmokka"));
words.add( Word("seven", "kenekaku"));
words.add( Word("eight", "kawinta"));
words.add( Word("nine", "wo’e"));
words.add( Word("ten", "na’aacha"));

val adapter = WordAdapter(this,words)

val listView = findViewById(R.id.list) as ListView?

listView!!.setAdapter(adapter)
}
}


The WordAdapter.kt file:-



package com.example.android.miwok

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.app.Activity
import android.content.Context
import android.widget.TextView
import android.view.LayoutInflater


class WordAdapter : ArrayAdapter<Word>() {

fun WordAdapter(context: Activity, words: ArrayList<Word>){
super.ArrayAdapter<Word>(context, 0, words)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Check if the existing view is being reused, otherwise inflate the view
var listItemView = convertView
if (listItemView == null) {
listItemView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false)
}

val currentWord = getItem(position)

val nameTextView = listItemView!!.findViewById(R.id.miwok_text_view) as TextView

nameTextView.setText(currentWord!!.getDefaultTranslation())

val numberTextView = listItemView.findViewById(R.id.default_text_view) as TextView

numberTextView.setText(currentWord!!.getMiwokTranslation())

return listItemView
}
}




I am not able to find the error in this.



The word super should be linked to which part is also confusable to me.










share|improve this question




















  • 1




    Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
    – Zoe
    yesterday










  • @Zoe, he wants to show more than one view per row.
    – Andre Artus
    yesterday










  • @AndreArtus so? Just initializing as ArrayAdapter is more than enough
    – Zoe
    yesterday










  • @Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
    – Andre Artus
    yesterday










  • The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
    – Zoe
    yesterday















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I was trying to send the word ArrayList in other ArrayDapter class, but the error arises that it has too many arguments and super keyword in kotlin can only be used on the left side of "." operator.



    package com.example.android.miwok

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.ArrayAdapter
import android.widget.ListView


class NumbersActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_numbers)

val words = ArrayList<Word>()
words.add(Word("one","lutti"))
words.add( Word("two", "otiiko"));
words.add( Word("three", "tolookosu"));
words.add( Word("four", "oyyisa"));
words.add( Word("five", "massokka"));
words.add( Word("six", "temmokka"));
words.add( Word("seven", "kenekaku"));
words.add( Word("eight", "kawinta"));
words.add( Word("nine", "wo’e"));
words.add( Word("ten", "na’aacha"));

val adapter = WordAdapter(this,words)

val listView = findViewById(R.id.list) as ListView?

listView!!.setAdapter(adapter)
}
}


The WordAdapter.kt file:-



package com.example.android.miwok

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.app.Activity
import android.content.Context
import android.widget.TextView
import android.view.LayoutInflater


class WordAdapter : ArrayAdapter<Word>() {

fun WordAdapter(context: Activity, words: ArrayList<Word>){
super.ArrayAdapter<Word>(context, 0, words)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Check if the existing view is being reused, otherwise inflate the view
var listItemView = convertView
if (listItemView == null) {
listItemView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false)
}

val currentWord = getItem(position)

val nameTextView = listItemView!!.findViewById(R.id.miwok_text_view) as TextView

nameTextView.setText(currentWord!!.getDefaultTranslation())

val numberTextView = listItemView.findViewById(R.id.default_text_view) as TextView

numberTextView.setText(currentWord!!.getMiwokTranslation())

return listItemView
}
}




I am not able to find the error in this.



The word super should be linked to which part is also confusable to me.










share|improve this question















I was trying to send the word ArrayList in other ArrayDapter class, but the error arises that it has too many arguments and super keyword in kotlin can only be used on the left side of "." operator.



    package com.example.android.miwok

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.ArrayAdapter
import android.widget.ListView


class NumbersActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_numbers)

val words = ArrayList<Word>()
words.add(Word("one","lutti"))
words.add( Word("two", "otiiko"));
words.add( Word("three", "tolookosu"));
words.add( Word("four", "oyyisa"));
words.add( Word("five", "massokka"));
words.add( Word("six", "temmokka"));
words.add( Word("seven", "kenekaku"));
words.add( Word("eight", "kawinta"));
words.add( Word("nine", "wo’e"));
words.add( Word("ten", "na’aacha"));

val adapter = WordAdapter(this,words)

val listView = findViewById(R.id.list) as ListView?

listView!!.setAdapter(adapter)
}
}


The WordAdapter.kt file:-



package com.example.android.miwok

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.app.Activity
import android.content.Context
import android.widget.TextView
import android.view.LayoutInflater


class WordAdapter : ArrayAdapter<Word>() {

fun WordAdapter(context: Activity, words: ArrayList<Word>){
super.ArrayAdapter<Word>(context, 0, words)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// Check if the existing view is being reused, otherwise inflate the view
var listItemView = convertView
if (listItemView == null) {
listItemView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false)
}

val currentWord = getItem(position)

val nameTextView = listItemView!!.findViewById(R.id.miwok_text_view) as TextView

nameTextView.setText(currentWord!!.getDefaultTranslation())

val numberTextView = listItemView.findViewById(R.id.default_text_view) as TextView

numberTextView.setText(currentWord!!.getMiwokTranslation())

return listItemView
}
}




I am not able to find the error in this.



The word super should be linked to which part is also confusable to me.







android inheritance kotlin super






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Zoe

10.1k73475




10.1k73475










asked yesterday









Priyanshu Vaya

42




42








  • 1




    Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
    – Zoe
    yesterday










  • @Zoe, he wants to show more than one view per row.
    – Andre Artus
    yesterday










  • @AndreArtus so? Just initializing as ArrayAdapter is more than enough
    – Zoe
    yesterday










  • @Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
    – Andre Artus
    yesterday










  • The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
    – Zoe
    yesterday
















  • 1




    Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
    – Zoe
    yesterday










  • @Zoe, he wants to show more than one view per row.
    – Andre Artus
    yesterday










  • @AndreArtus so? Just initializing as ArrayAdapter is more than enough
    – Zoe
    yesterday










  • @Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
    – Andre Artus
    yesterday










  • The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
    – Zoe
    yesterday










1




1




Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
– Zoe
yesterday




Why are you calling super.ArrayAdapter? Just use ArrayAdapter directly
– Zoe
yesterday












@Zoe, he wants to show more than one view per row.
– Andre Artus
yesterday




@Zoe, he wants to show more than one view per row.
– Andre Artus
yesterday












@AndreArtus so? Just initializing as ArrayAdapter is more than enough
– Zoe
yesterday




@AndreArtus so? Just initializing as ArrayAdapter is more than enough
– Zoe
yesterday












@Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
– Andre Artus
yesterday




@Zoe, would you mind putting up an example? By default ArrayAdapter only loads one TextView (in a ViewGroup).
– Andre Artus
yesterday












The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
– Zoe
yesterday






The point being super.ArrayAdapter<...>.... can be replaced with ArrayAdapter<...>.... As in removing the super. part. That's all there is to it
– Zoe
yesterday














1 Answer
1






active

oldest

votes

















up vote
0
down vote













There are a few things you can improve upon:



You are currently writing Javlin, or Kotva, i.e. a mix of Java and Kotlin.



You should not be inflating the layout for every getView, and you should ideally be using the ViewHolder pattern (now shown below).



But the reason for your question, the compiler error calling super. You are defining your constructor as a function, not a constructor.



In Java constructors are defined by writing methods with the same name as the class. In Kotlin you just pass the constructor parameters in the class definition. e.g.



class WordAdapter(context: Context, objects: List<Word>)


and pass those parameters through to the extended class as such:



class WordAdapter(context: Context, objects: List<Word>) :
ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects)


an alternative approach, usually reserved for secondary constructors:



class WordAdapter : ArrayAdapter<Word> {  
constructor(context: Context, objects: List<Word>) : super(
context, R.layout.list_item, R.id.default_text_view, objects
)
}


Here is a somewhat Kotlinified version of your code (I saw that you were swapping default and miwok in your getView, I assumed that was a mistake and did not copy that). I've also put a copy up on Github.



class MainActivity : AppCompatActivity() {

private val words = arrayListOf(
Word("one", "lutti"),
Word("two", "otiiko"),
Word("three", "tolookosu"),
Word("four", "oyyisa"),
Word("five", "massokka"),
Word("six", "temmokka"),
Word("seven", "kenekaku"),
Word("eight", "kawinta"),
Word("nine", "wo’e"),
Word("ten", "na’aacha")
)

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

val listView = findViewById<ListView>(R.id.list)
val adapter = WordAdapter(this, words)

listView.adapter = adapter
}
}

data class Word(val defaultTranslation: String, val miwokTranslation: String)

class WordAdapter(context: Context, objects: List<Word>) :
ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val listItemView = super.getView(position, convertView, parent)
val defaultTextView = listItemView.findViewById<TextView>(R.id.default_text_view)
val miwokTextView = listItemView.findViewById<TextView>(R.id.miwok_text_view)

getItem(position)?.run {
defaultTextView.text = defaultTranslation
miwokTextView.text = miwokTranslation
}
return listItemView
}
}


Here is an example adapter using (a version of) the ViewHolder pattern (if you put a logging breakpoint on any of the row.findViewByIds you will see they are only called for as many items as are being displayed, not for every item in the list):



class WordAdapter(context: Context, objects: List<Word>) :
ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

class ViewHolder(row: View) {
init {
row.tag = this
}

val defaultTextView = row.findViewById<TextView>(R.id.default_text_view)!!
val miwokTextView = row.findViewById<TextView>(R.id.miwok_text_view)!!
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val listItemView = super.getView(position, convertView, parent)

val holder = listItemView.tag as? ViewHolder ?: ViewHolder(listItemView)

getItem(position)?.run {
holder.defaultTextView.text = defaultTranslation
holder.miwokTextView.text = miwokTranslation
}

return listItemView
}
}


If you want to go overboard [:)] with Kotlin idioms:



override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
super.getView(position, convertView, parent).also { row ->
(row.tag as? ViewHolder ?: ViewHolder(row)).apply {
getItem(position)?.run {
defaultTextView.text = defaultTranslation
miwokTextView.text = miwokTranslation
}
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238407%2ferror-in-super-keyword-in-arrayadapter-in-kotlin%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    There are a few things you can improve upon:



    You are currently writing Javlin, or Kotva, i.e. a mix of Java and Kotlin.



    You should not be inflating the layout for every getView, and you should ideally be using the ViewHolder pattern (now shown below).



    But the reason for your question, the compiler error calling super. You are defining your constructor as a function, not a constructor.



    In Java constructors are defined by writing methods with the same name as the class. In Kotlin you just pass the constructor parameters in the class definition. e.g.



    class WordAdapter(context: Context, objects: List<Word>)


    and pass those parameters through to the extended class as such:



    class WordAdapter(context: Context, objects: List<Word>) :
    ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects)


    an alternative approach, usually reserved for secondary constructors:



    class WordAdapter : ArrayAdapter<Word> {  
    constructor(context: Context, objects: List<Word>) : super(
    context, R.layout.list_item, R.id.default_text_view, objects
    )
    }


    Here is a somewhat Kotlinified version of your code (I saw that you were swapping default and miwok in your getView, I assumed that was a mistake and did not copy that). I've also put a copy up on Github.



    class MainActivity : AppCompatActivity() {

    private val words = arrayListOf(
    Word("one", "lutti"),
    Word("two", "otiiko"),
    Word("three", "tolookosu"),
    Word("four", "oyyisa"),
    Word("five", "massokka"),
    Word("six", "temmokka"),
    Word("seven", "kenekaku"),
    Word("eight", "kawinta"),
    Word("nine", "wo’e"),
    Word("ten", "na’aacha")
    )

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

    val listView = findViewById<ListView>(R.id.list)
    val adapter = WordAdapter(this, words)

    listView.adapter = adapter
    }
    }

    data class Word(val defaultTranslation: String, val miwokTranslation: String)

    class WordAdapter(context: Context, objects: List<Word>) :
    ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val listItemView = super.getView(position, convertView, parent)
    val defaultTextView = listItemView.findViewById<TextView>(R.id.default_text_view)
    val miwokTextView = listItemView.findViewById<TextView>(R.id.miwok_text_view)

    getItem(position)?.run {
    defaultTextView.text = defaultTranslation
    miwokTextView.text = miwokTranslation
    }
    return listItemView
    }
    }


    Here is an example adapter using (a version of) the ViewHolder pattern (if you put a logging breakpoint on any of the row.findViewByIds you will see they are only called for as many items as are being displayed, not for every item in the list):



    class WordAdapter(context: Context, objects: List<Word>) :
    ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

    class ViewHolder(row: View) {
    init {
    row.tag = this
    }

    val defaultTextView = row.findViewById<TextView>(R.id.default_text_view)!!
    val miwokTextView = row.findViewById<TextView>(R.id.miwok_text_view)!!
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val listItemView = super.getView(position, convertView, parent)

    val holder = listItemView.tag as? ViewHolder ?: ViewHolder(listItemView)

    getItem(position)?.run {
    holder.defaultTextView.text = defaultTranslation
    holder.miwokTextView.text = miwokTranslation
    }

    return listItemView
    }
    }


    If you want to go overboard [:)] with Kotlin idioms:



    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
    super.getView(position, convertView, parent).also { row ->
    (row.tag as? ViewHolder ?: ViewHolder(row)).apply {
    getItem(position)?.run {
    defaultTextView.text = defaultTranslation
    miwokTextView.text = miwokTranslation
    }
    }
    }





    share|improve this answer



























      up vote
      0
      down vote













      There are a few things you can improve upon:



      You are currently writing Javlin, or Kotva, i.e. a mix of Java and Kotlin.



      You should not be inflating the layout for every getView, and you should ideally be using the ViewHolder pattern (now shown below).



      But the reason for your question, the compiler error calling super. You are defining your constructor as a function, not a constructor.



      In Java constructors are defined by writing methods with the same name as the class. In Kotlin you just pass the constructor parameters in the class definition. e.g.



      class WordAdapter(context: Context, objects: List<Word>)


      and pass those parameters through to the extended class as such:



      class WordAdapter(context: Context, objects: List<Word>) :
      ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects)


      an alternative approach, usually reserved for secondary constructors:



      class WordAdapter : ArrayAdapter<Word> {  
      constructor(context: Context, objects: List<Word>) : super(
      context, R.layout.list_item, R.id.default_text_view, objects
      )
      }


      Here is a somewhat Kotlinified version of your code (I saw that you were swapping default and miwok in your getView, I assumed that was a mistake and did not copy that). I've also put a copy up on Github.



      class MainActivity : AppCompatActivity() {

      private val words = arrayListOf(
      Word("one", "lutti"),
      Word("two", "otiiko"),
      Word("three", "tolookosu"),
      Word("four", "oyyisa"),
      Word("five", "massokka"),
      Word("six", "temmokka"),
      Word("seven", "kenekaku"),
      Word("eight", "kawinta"),
      Word("nine", "wo’e"),
      Word("ten", "na’aacha")
      )

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

      val listView = findViewById<ListView>(R.id.list)
      val adapter = WordAdapter(this, words)

      listView.adapter = adapter
      }
      }

      data class Word(val defaultTranslation: String, val miwokTranslation: String)

      class WordAdapter(context: Context, objects: List<Word>) :
      ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
      val listItemView = super.getView(position, convertView, parent)
      val defaultTextView = listItemView.findViewById<TextView>(R.id.default_text_view)
      val miwokTextView = listItemView.findViewById<TextView>(R.id.miwok_text_view)

      getItem(position)?.run {
      defaultTextView.text = defaultTranslation
      miwokTextView.text = miwokTranslation
      }
      return listItemView
      }
      }


      Here is an example adapter using (a version of) the ViewHolder pattern (if you put a logging breakpoint on any of the row.findViewByIds you will see they are only called for as many items as are being displayed, not for every item in the list):



      class WordAdapter(context: Context, objects: List<Word>) :
      ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

      class ViewHolder(row: View) {
      init {
      row.tag = this
      }

      val defaultTextView = row.findViewById<TextView>(R.id.default_text_view)!!
      val miwokTextView = row.findViewById<TextView>(R.id.miwok_text_view)!!
      }

      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
      val listItemView = super.getView(position, convertView, parent)

      val holder = listItemView.tag as? ViewHolder ?: ViewHolder(listItemView)

      getItem(position)?.run {
      holder.defaultTextView.text = defaultTranslation
      holder.miwokTextView.text = miwokTranslation
      }

      return listItemView
      }
      }


      If you want to go overboard [:)] with Kotlin idioms:



      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
      super.getView(position, convertView, parent).also { row ->
      (row.tag as? ViewHolder ?: ViewHolder(row)).apply {
      getItem(position)?.run {
      defaultTextView.text = defaultTranslation
      miwokTextView.text = miwokTranslation
      }
      }
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        There are a few things you can improve upon:



        You are currently writing Javlin, or Kotva, i.e. a mix of Java and Kotlin.



        You should not be inflating the layout for every getView, and you should ideally be using the ViewHolder pattern (now shown below).



        But the reason for your question, the compiler error calling super. You are defining your constructor as a function, not a constructor.



        In Java constructors are defined by writing methods with the same name as the class. In Kotlin you just pass the constructor parameters in the class definition. e.g.



        class WordAdapter(context: Context, objects: List<Word>)


        and pass those parameters through to the extended class as such:



        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects)


        an alternative approach, usually reserved for secondary constructors:



        class WordAdapter : ArrayAdapter<Word> {  
        constructor(context: Context, objects: List<Word>) : super(
        context, R.layout.list_item, R.id.default_text_view, objects
        )
        }


        Here is a somewhat Kotlinified version of your code (I saw that you were swapping default and miwok in your getView, I assumed that was a mistake and did not copy that). I've also put a copy up on Github.



        class MainActivity : AppCompatActivity() {

        private val words = arrayListOf(
        Word("one", "lutti"),
        Word("two", "otiiko"),
        Word("three", "tolookosu"),
        Word("four", "oyyisa"),
        Word("five", "massokka"),
        Word("six", "temmokka"),
        Word("seven", "kenekaku"),
        Word("eight", "kawinta"),
        Word("nine", "wo’e"),
        Word("ten", "na’aacha")
        )

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

        val listView = findViewById<ListView>(R.id.list)
        val adapter = WordAdapter(this, words)

        listView.adapter = adapter
        }
        }

        data class Word(val defaultTranslation: String, val miwokTranslation: String)

        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val listItemView = super.getView(position, convertView, parent)
        val defaultTextView = listItemView.findViewById<TextView>(R.id.default_text_view)
        val miwokTextView = listItemView.findViewById<TextView>(R.id.miwok_text_view)

        getItem(position)?.run {
        defaultTextView.text = defaultTranslation
        miwokTextView.text = miwokTranslation
        }
        return listItemView
        }
        }


        Here is an example adapter using (a version of) the ViewHolder pattern (if you put a logging breakpoint on any of the row.findViewByIds you will see they are only called for as many items as are being displayed, not for every item in the list):



        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

        class ViewHolder(row: View) {
        init {
        row.tag = this
        }

        val defaultTextView = row.findViewById<TextView>(R.id.default_text_view)!!
        val miwokTextView = row.findViewById<TextView>(R.id.miwok_text_view)!!
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val listItemView = super.getView(position, convertView, parent)

        val holder = listItemView.tag as? ViewHolder ?: ViewHolder(listItemView)

        getItem(position)?.run {
        holder.defaultTextView.text = defaultTranslation
        holder.miwokTextView.text = miwokTranslation
        }

        return listItemView
        }
        }


        If you want to go overboard [:)] with Kotlin idioms:



        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
        super.getView(position, convertView, parent).also { row ->
        (row.tag as? ViewHolder ?: ViewHolder(row)).apply {
        getItem(position)?.run {
        defaultTextView.text = defaultTranslation
        miwokTextView.text = miwokTranslation
        }
        }
        }





        share|improve this answer














        There are a few things you can improve upon:



        You are currently writing Javlin, or Kotva, i.e. a mix of Java and Kotlin.



        You should not be inflating the layout for every getView, and you should ideally be using the ViewHolder pattern (now shown below).



        But the reason for your question, the compiler error calling super. You are defining your constructor as a function, not a constructor.



        In Java constructors are defined by writing methods with the same name as the class. In Kotlin you just pass the constructor parameters in the class definition. e.g.



        class WordAdapter(context: Context, objects: List<Word>)


        and pass those parameters through to the extended class as such:



        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects)


        an alternative approach, usually reserved for secondary constructors:



        class WordAdapter : ArrayAdapter<Word> {  
        constructor(context: Context, objects: List<Word>) : super(
        context, R.layout.list_item, R.id.default_text_view, objects
        )
        }


        Here is a somewhat Kotlinified version of your code (I saw that you were swapping default and miwok in your getView, I assumed that was a mistake and did not copy that). I've also put a copy up on Github.



        class MainActivity : AppCompatActivity() {

        private val words = arrayListOf(
        Word("one", "lutti"),
        Word("two", "otiiko"),
        Word("three", "tolookosu"),
        Word("four", "oyyisa"),
        Word("five", "massokka"),
        Word("six", "temmokka"),
        Word("seven", "kenekaku"),
        Word("eight", "kawinta"),
        Word("nine", "wo’e"),
        Word("ten", "na’aacha")
        )

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

        val listView = findViewById<ListView>(R.id.list)
        val adapter = WordAdapter(this, words)

        listView.adapter = adapter
        }
        }

        data class Word(val defaultTranslation: String, val miwokTranslation: String)

        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val listItemView = super.getView(position, convertView, parent)
        val defaultTextView = listItemView.findViewById<TextView>(R.id.default_text_view)
        val miwokTextView = listItemView.findViewById<TextView>(R.id.miwok_text_view)

        getItem(position)?.run {
        defaultTextView.text = defaultTranslation
        miwokTextView.text = miwokTranslation
        }
        return listItemView
        }
        }


        Here is an example adapter using (a version of) the ViewHolder pattern (if you put a logging breakpoint on any of the row.findViewByIds you will see they are only called for as many items as are being displayed, not for every item in the list):



        class WordAdapter(context: Context, objects: List<Word>) :
        ArrayAdapter<Word>(context, R.layout.list_item, R.id.default_text_view, objects) {

        class ViewHolder(row: View) {
        init {
        row.tag = this
        }

        val defaultTextView = row.findViewById<TextView>(R.id.default_text_view)!!
        val miwokTextView = row.findViewById<TextView>(R.id.miwok_text_view)!!
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val listItemView = super.getView(position, convertView, parent)

        val holder = listItemView.tag as? ViewHolder ?: ViewHolder(listItemView)

        getItem(position)?.run {
        holder.defaultTextView.text = defaultTranslation
        holder.miwokTextView.text = miwokTranslation
        }

        return listItemView
        }
        }


        If you want to go overboard [:)] with Kotlin idioms:



        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View =
        super.getView(position, convertView, parent).also { row ->
        (row.tag as? ViewHolder ?: ViewHolder(row)).apply {
        getItem(position)?.run {
        defaultTextView.text = defaultTranslation
        miwokTextView.text = miwokTranslation
        }
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Andre Artus

        1,4381118




        1,4381118






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238407%2ferror-in-super-keyword-in-arrayadapter-in-kotlin%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine