Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Custom ListViews Setting the Data

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Help with Kotlin and KotlinX

I am trying to follow this course in Kotlin. However, convertView is declared as a val. This makes it impossible to assign convertView within the getView method. Please help me with a workaround that makes full use of kotlin-android-extensions

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

It would be really helpful if there was an explanation about how everything works, which method does the ListView call and when.

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

Since the idea is to either create a new view or reuse the old one if it exists and eventually return that view at the end of the method, it would make sense to declare a new View var at the top to use in place of convertView

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Please guide me if I have made a mistake. Also, please tell me where the ViewHolder fits in.

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.example.raveesh.stormy.R
import com.example.raveesh.stormy.weather.Day
import kotlinx.android.synthetic.main.daily_list_item.view.*
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.layoutInflater

class DayAdapter(mContext: Context,
                        private val mDays: Array<Day>) : BaseAdapter() {
    private val inflater = mContext.layoutInflater
    lateinit var view :View

    override fun getItem(position: Int): Any = mDays[position]
    override fun getCount(): Int = mDays.size
    override fun getItemId(position: Int): Long = 0//not used

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        view =
                if (convertView == null) inflater.inflate(
                        R.layout.daily_list_item,
                        parent,
                        false
                ) else convertView

        view.iconImageView.imageResource = mDays[position].getIconId()
        view.temperatureLabel.text = mDays[position].mTemperatureMax.toInt().toString()
        view.dayNameLabel.text = mDays[position].getDayOfTheWeek()
        return view
    }

}

In all the java examples, we have returned convertView but in kotlin, convertView is a val and cannot be reassigned. To return convertView as a View? returns a Null Pointer exception and to accept convertView as a View and not View? , the value of convertView can never be null, so please guide me if I am doing anything wrong. Please also comment on returning view instead of convertView thanks a lot

Seth Kroger
Seth Kroger
56,413 Points

I think that looks good.

Take a look at https://kotlinlang.org/docs/reference/nested-classes.html for the syntax of doing the ViewHolder. A Java static inner class is a Kotlin nested class while a regular Java inner class is a Kotlin inner class.