I am new on kotlin .Getting error on click listner on line holder.initilise(chapterlist.get(position),clickListener)
Error shows on word clickListener . Kindly help me to resolve my issue. i want to click on one view and transfer on another recyclerView. These list shows chapters i want to go on topics list and every chapter has its own topics.holder.initilise(chapterlist.get(position),clickListener) is commented in the code and it just shows the list which i extract from backend
Code :
import android.content.Context
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class CustomeAdapter(val ctx: Context, var clickListener: Int, var chapterlist:ArrayList<Model>) : RecyclerView.Adapter<ViewHolder>() {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user : Model = chapterlist[position]
holder?.textViewName?.text=user.name
holder?.textViewChapter?.text=user.desc
// holder.initilise(chapterlist.get(position),clickListener) //(getting error on clickListener)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent?.context).inflate(R.layout.row,parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return chapterlist.size
}
}
class ViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
val textViewName = itemView.findViewById(R.id.textView) as TextView
val textViewChapter = itemView.findViewById(R.id.textView2) as TextView
fun initilise(list: Model, action:OnChapterClick){
textViewName.text=list.name
textViewChapter.text=list.desc
itemView.setOnClickListener{
action.onItemClick(list,adapterPosition)
}
}
}
interface OnChapterClick {
fun onItemClick(list: Model,position: Int )
}
Follow below steps:
Step - 1: You have to implements OnChapterClick in your MainActivity2
class MainActivity2: AppCompatActivity, OnChapterClick {
....
override fun onItemClick(list: Model, position: Int ) {
//Implement your logic here
}
}
Step - 2: Change your CustomeAdapter's constructor to accept OnChapterClick instead of int
class CustomeAdapter(val ctx: Context, var clickListener: OnChapterClick, var chapterlist:ArrayList<Model>) : RecyclerView.Adapter<ViewHolder>() {
....
}
Step - 3: Initialize the adapter with this instead of R.layout.row like below:
val adapter = CustomeAdapter(applicationContext, this#MainActivity2, chapterlist)
Your clickListener is defined to be an Int:
var clickListener: Int
That's probably wrong, since integers can't listen to clicks. It should probably be:
var clickListener: OnChapterClick
Related
I just started learning Kotlin
i would like to start a new activitys from a RecyclerView with Kotlin.
for example :
if user tap on first item go to activity1
if user tap on secend item go to activity2
and...
i did make adaptor and adaptor will show in a feragment and there isnt any issue with start adaptor
my adaptor code:
package com.mysfk.android
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ReviewAdaptor() :RecyclerView.Adapter<ReviewAdaptor.ViewHolder>() {
private var titles = arrayOf("بخاری","دریچه","فن و پد","آبیاری","فن چرخشی","مه پاش","پرده")
private var details = arrayOf("جزیئات تست","جزیئات تست","جزیئات تست","جزیئات تست","جزیئات تست","جزیئات تست","جزیئات تست")
private var images = intArrayOf(R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReviewAdaptor.ViewHolder {
val v =LayoutInflater.from(parent.context).inflate(R.layout.card_review,parent,false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: ReviewAdaptor.ViewHolder, position: Int) {
holder.tittleItem.text = titles[position]
holder.ditealItem.text = details[position]
holder.imageItem.setImageResource(images[position])
}
override fun getItemCount(): Int {
return titles.size
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
var imageItem : ImageView
var tittleItem : TextView
var ditealItem :TextView
init {
imageItem = itemView.findViewById(R.id.imageReview)
tittleItem = itemView.findViewById(R.id.titleReview)
ditealItem = itemView.findViewById(R.id.detReview)
itemView.setOnClickListener{
}
}
}
}
and this is my items:
In ReviewAdaptor declare a listener:
private lateinit var itemClickListener: View.OnClickListener
public fun setItemClickListener(listener: View.OnClickListener) {
itemClickListener = listener
}
In your ViewHolder:
itemView.tag = this
itemView.setOnClickListener(itemClickListener)
And in your fragment:
reviewAdaptor.setItemClickListener{
val viewHolder: RecyclerView.ViewHolder = it.tag as RecyclerView.ViewHolder
val position = viewHolder.adapterPosition
// Open activity by postion
}
I am not able to figure out how to change the value of alpha when clicked on the item of the RecyclerView . I want to change the value of alpha from 1 to 0.5 .
Below is the Adapter Class for the same .
package GiftClass
import LeaderboardClass.LeaderboardAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gearsrun.www.R
import kotlinx.android.synthetic.main.item_gift.view.*
class GiftAdapter(private val giftList: List<Gift>) : RecyclerView.Adapter<GiftAdapter.GiftViewHolder>() {
private lateinit var mlistener : onItemClickListener
interface onItemClickListener{
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener){
mlistener = listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GiftViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_gift,parent,false)
return GiftViewHolder(itemView,mlistener)
}
override fun onBindViewHolder(holder: GiftViewHolder, position: Int) {
val currentItem = giftList[position]
holder.item_img.setImageResource(currentItem.imageResource)
holder.item_price.text = currentItem.price
holder.item_soldNum.text = currentItem.sold_num
holder.item_medal.text = currentItem.medal_num
}
override fun getItemCount() = giftList.size
class GiftViewHolder(itemView: View,listener:onItemClickListener) : RecyclerView.ViewHolder(itemView){
val item_img:ImageView = itemView.item_img
val item_price : TextView = itemView.price
val item_soldNum : TextView = itemView.sold_num
val item_medal : TextView = itemView.medal_num
init {
itemView.setOnClickListener {
listener.onItemClick(absoluteAdapterPosition)
itemView.alpha = 0.5f
}
}
}
}
Added References / Suggestion are appreciated
In the onClickListener , you can get the property itemView.alpha and adjust the alpha values as per your needs
My Main Activity Class
This is implemented to learn recycler view and to handle clicks. The below code works fine but while implementing listener I got confused. All the doubts are listed below. Do help.
package com.suasnom.pizzarecyclerview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), isClickedInterface {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//implementing recycler view
recycler_view.layoutManager= LinearLayoutManager(this)
val data = fetchData()
val adapter = CustomAdapter(data, this)
recycler_view.adapter = adapter
}
fun fetchData(): ArrayList<String> {
val list_Strings = ArrayList<String>()
var str = ""
for(i in 1..100){
str = "${i} line"
list_Strings.add(str)
}
return list_Strings
}
override fun onItemClicked(item: String) {
Toast.makeText(this, "$item", Toast.LENGTH_LONG).show()
}
}
In this statement I passed
val adapter = CustomAdapter(data, this)
and it allows me to override the below method:
override fun onItemClicked(item: String) {
Toast.makeText(this, "$item", Toast.LENGTH_LONG).show()
}
The below code is for recycler view adapter where I write that interface:
package com.suasnom.pizzarecyclerview
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class CustomAdapter(val list_strings: ArrayList<String>, private val listner: isClickedInterface): RecyclerView.Adapter<PizzaViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PizzaViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.row, parent, false)
val pizzaObject = PizzaViewHolder(view)
view.setOnClickListener {
listner.onItemClicked(list_strings[pizzaObject.adapterPosition])
}
return pizzaObject
}
override fun onBindViewHolder(holder: PizzaViewHolder, position: Int) {
val data_incoming = list_strings[position]
holder.text_message.text = data_incoming
}
override fun getItemCount(): Int {
return list_strings.size
}
}
class PizzaViewHolder(private val view: View): RecyclerView.ViewHolder(view){
val text_message = view.findViewById<TextView>(R.id.textrow)
}
interface isClickedInterface{
fun onItemClicked(item: String){}
}
Any idea how this is working. Please Help ...
inside CustomAdapter on the bottom you have declared isClickedInterface (it might be declared anywhere else or as separated file). it is implemented by your MainActivity (after :), so you have to set this interface methods inside implementing class - so in Activity appears onItemClicked(item: String) method
now your CustomAdapter have constructor param to pass this interface (second one). for initiating new instance of adapter you have pass implemented interface, in here you may pass whole Activity as it implements desired interface (val adapter = CustomAdapter(data, this) - this points on Activity, which is also an isClickedInterface interface instance)
now inside onCreateViewHolder you are setting setOnClickListener and inside of it you are calling method from passed interface in constructor
I'm developing an android app in kotlin, and I want to have a button in every recyclerView element, which will launch an intent - the same in whole recycle view, but with different parameters(for now it's just position for testing, in final form that will be some value from database).
I write the following code for that(inside my adapter class):
override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
val Edit: Button = holder.view.EditButton
Edit.setOnClickListener()
{
var projekt: Intent = Intent(applicationContext, Project::class.java)
projekt.putExtra("id", position)
startActivity(projekt)
}
But I get "unresolved refference" error for applicationContext. I used buttons with intent like that before and that worked perfectly fine, though this is the first time I'm trying to do it inside recyclerView element.
How to make it work? Maybe I just take the wrong approach and it should be done in different way?
Edit: Complete adapter class file:
package com.example.legoapp127260
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.project_item_layout.view.*
class ProjectAdapter : RecyclerView.Adapter<ProjectViewHolder>()
{
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ProjectViewHolder {
val layoutInflater = LayoutInflater.from(viewGroup.context)
val projectRow = layoutInflater.inflate(R.layout.project_item_layout, viewGroup, false)
return ProjectViewHolder(projectRow)
}
override fun getItemCount(): Int {
return 2;
}
override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
val projectName: TextView = holder.view.projectName
val projectNames: Array<String> = arrayOf("Set 1", "Set 2")
val Edit: Button = holder.view.EditButton
projectName.setText(projectNames[position])
Edit.setOnClickListener()
{
var projekt: Intent = Intent(Edit.context, Project::class.java)
projekt.putExtra("id", position)
Edit.context.startActivity(projekt)
}
}
}
class ProjectViewHolder(val view: View) : RecyclerView.ViewHolder(view)
{
}
You can get context from your button:
var projekt: Intent = Intent(Edit.context, Project::class.java)
projekt.putExtra("id", position)
Edit.context.startActivity(projekt)
Please help me, I can't implement onBindViewHolder in my RecyclerView in Kotlin. I want to make an adapter for my data cards using Reyclerview in Kotlin language android studio. But when I implement onBindViewHolder my code throws an error. Please help.
package com.brid.azis.vipgame.test.Adapter
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import android.widget.TextView
import com.brid.azis.vipgame.R
import com.brid.azis.vipgame.test.DataModel.DataCard
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
CardViewHolder(LayoutInflater.from(context).inflate(R.layout.item_mission,parent,false))
override fun getItemCount(): Int = cards.size
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
holder.bindCards(cards[position])
}
class CardViewHolder(view:View):RecyclerView.ViewHolder(view) {
val judul = view.findViewById<TextView>(R.id.tv_judulkartu)
val petunjuk = view.findViewById<TextView>(R.id.tv_petunjukkartu)
val tanggal = view.findViewById<TextView>(R.id.tv_tanggalmasukkartu)
fun bindCards(cards:DataCard) {
judul.text = cards.judul
petunjuk.text = cards.petunjuk
tanggal.text = cards.tanggal
}
}
}
This is the error:
Just change this line
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
to
class MissionViewAdapter(private val context: Context, private val cards:List<DataCard>):
RecyclerView.Adapter<MissionViewAdapter.CardViewHolder>() {
Actually, you are using the main ViewHolder class of RecyclerView. That's why your parameters are wrong of bindViewHolder method and you are getting that error. In this case, your onBindViewHolder method should look like this:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {