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 trialRaveesh Agarwal
2,994 PointsGetting 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
Treehouse TeacherHey 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
2,994 PointsRaveesh Agarwal
2,994 PointsThanks 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!