Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Acting on List Item Taps Tapping on a RecyclerView Item

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Getting context

Hi, Thanks, Ben! Implementing the OnClickListener this way makes much more sense! However, my only doubt is the way in which we get the context for the toast. I believe it makes more sense to get the context from the view that was clicked. Also, just like that, in onCreateViewHolder, it makes more sense to me to get the context from the ViewGroup the RecyclerView passed in. Please have a look and suggest if I'm making a mistake.

package com.example.raveesh.stormy.adapters

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.Adapter
import android.view.View
import android.view.ViewGroup
import com.example.raveesh.stormy.R
import com.example.raveesh.stormy.weather.Hour
import kotlinx.android.synthetic.main.hourly_list_item.view.*
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.layoutInflater
import org.jetbrains.anko.longToast

class HourAdapter(private val mHours : Array<Hour>) : Adapter<HourAdapter.HourViewHolder>(){

    private lateinit var mHour : Hour
    override fun onBindViewHolder(holder: HourViewHolder, position: Int) {
        mHour = mHours[position]
        holder.mIconView.imageResource = mHour.getIconId()
        holder.mSummaryLabel.text = mHour.mSummary
        holder.mTemperatureLabel.text = mHour.getTemperature()
        holder.mTimeLabel.text = mHour.getTime()
    }

    override fun getItemCount(): Int = mHours.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HourViewHolder {
        val v = parent
                .context
                .layoutInflater
                .inflate(R.layout.hourly_list_item, parent, false)
        return HourViewHolder(v)
    }

    class HourViewHolder(itemView: View) :
            RecyclerView.ViewHolder(itemView),
            View.OnClickListener  {

        init { itemView.setOnClickListener(this) }
        val mIconView = itemView.hourlyIconView!!
        val mSummaryLabel = itemView.hourlySummaryLabel!!
        val mTemperatureLabel = itemView.hourlyTemperatureLabel!!
        val mTimeLabel = itemView.hourlyTimeLabel!!

        override fun onClick(p0: View) {
            p0.context.longToast(String.format("At %s the it will be %s and %s"
                    , mTimeLabel.text
                    , mTemperatureLabel.text
                    , mSummaryLabel.text)
            )
        }
    }

}

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Raveesh!

That sounds fine to me. Either way you'd be using the same Context, so I think it's just a personal preference about where to get the Context from.

Hope that helps!

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Thanks a lot for your guidance! I am just learning and trying to get hold of the concepts. And that I made a modification, I just needed to confirm if I am doing anything wrong. Thanks a lot for this!