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)
Related
MyAdapter.kt
im a beginner in this and how to open another activity when i click the cardview..previously it worked fine,but when i click the card nothing happens and only toast show up
I want the user to be able to click on a card and go to a different activity. If you click on card 1 you must go to the activity1. If you click on card 2 you must go to the activity2. Etc...
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.viewpager.widget.PagerAdapter
import kotlinx.android.synthetic.main.card_item.view.*
class MyAdapter(private val context: Context, private val myModelArrayList: ArrayList<MyModel>) : PagerAdapter(){
override fun getCount(): Int {
return myModelArrayList.size //return list of records/items
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view == `object`
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
//inflate layout card_item.xml
val view = LayoutInflater.from(context).inflate(R.layout.card_item, container, false)
//get data
val model = myModelArrayList.get(position)
val title = model.title
val description = model.description
val date = model.date
val image = model.image
//set data to ui views
view.bannerIv.setImageResource(image)
view.titleTv.text = title
view.descriptionTv.text = description
view.dateTv.text = date
view.setOnClickListener {
val intent = Intent(context, SecondActivity::class.java)
context.startActivity(intent)
// finish();
}
//handle item/card click
view.setOnClickListener{
Toast.makeText(context,"$title \n $description \n $date", Toast.LENGTH_SHORT).show()
}
//add view to container
container.addView(view, position)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View)
}
}
```
just simply remove your second view.setOnClickListener and move your toast to first view.setOnClickListener. As others mentioned second listener will be overridden.
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
Here is my code :-
Favourite Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.security.AccessController.getContext
//this is my calling activity
class FavouriteActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.favourite_activity)
val mToolbar: Toolbar = findViewById(R.id.toolbar_favourite)
setSupportActionBar(mToolbar)
getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
getSupportActionBar()?.setDisplayShowHomeEnabled(true);
setTitle("Favourite Activity");
//getting recyclerview from xml
val recyclerView = findViewById(R.id.recyclerView) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//it can be staggered and grid
//creating our adapter
val adapter = CustomAdapter(star) //here I am calling the adapter activity
//now adding the adapter to recyclerview
recyclerView.adapter = adapter
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
}
CustomAdapter class
class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//this method is returning the view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout_favourite, parent, false)
return ViewHolder(v)
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
holder.imgCopy.setOnClickListener(View.OnClickListener {
holder.shareString(userList[position])
Toast.makeText(holder.itemView.getContext(),"Copy Button Clicked", Toast.LENGTH_SHORT).show()
})
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return userList.size
}
//the class is holding the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imgCopy: ImageView = itemView.findViewById(R.id.img_copy) as ImageView
val textViewName = itemView.findViewById(R.id.tvTitle) as TextView
fun bindItems(user: User) {
textViewName.text = user.name
}
fun shareString(user: User)
{
val message : String = user.name
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT,message)
intent.type = "text/plain"
startActivity(Intent.createChooser(intent,"Share to :")) ///Issue occur right here
}}}
Getting error : Required context , found Intent.
it is working fine in other FragmentActivity.
I have tried various methods to called the context. but anything is not working.
I have also passed the context from Fragment activity, but that also not worked.
Please let me know is there any way to start Intent.
As I am always getting error and stuck due to this.
The startActivity available in the ViewHolder class is different from the one available in activites. So in this method (available in viewholder), the first parameter should be a context. So pass the context as follows:
startActivity(itemView.context, Intent.createChooser(intent,"Share to :"))
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
I'am trying to handle my recyclerview put there is a problem
when i scroll down (onBindView function) works fine
but when scroll back to the first items in recyclerview everything becomes wrong like the following images.
package com.leaderspro.mrlawyer.adapters
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.leaderspro.mrlawyer.R
import com.leaderspro.mrlawyer.models.TODOModel
import kotlinx.android.synthetic.main.todo_list.view.*
class TODOAdapter(private val mArray: ArrayList<TODOModel>) :
RecyclerView.Adapter<TODOAdapter.ViewHolder>() {
var mView: View? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
mView = LayoutInflater.from(parent.context).inflate(R.layout.todo_list, parent, false)
return ViewHolder(mView!!)
}
override fun getItemCount(): Int {
return mArray.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val mTODO = mArray[position]
if (mTODO.isDone == 0) {//not complete
holder.ivIsDone.setImageResource(R.drawable.ic_checkbox_unchecked)
} else if (mTODO.isDone == 1) {
holder.ivIsDone.setImageResource(R.drawable.ic_checkbox_checked)
holder.tvTodoTask.paintFlags =
holder.tvTodoTask.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG //put line on done Tasks
}
holder.tvTodoTask.text = mTODO.task
holder.tvTODODate.text = mTODO.date
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvTodoTask = itemView.tvTodoTask!!
val tvTODODate = itemView.tvTODODate!!
val ivIsDone = itemView.ivIsDone!!
val todoListMainLinear = itemView.todoListMainLinear!!
}
}
works fine
https://i.stack.imgur.com/6mBUJ.png
works fine
https://i.stack.imgur.com/qcpLK.png
Wrongly called onBindView when Scroll Back
https://i.stack.imgur.com/2sRsu.png
use holder.adapterposition instead of position
val mTODO = mArray[holder.adapterposition]
for more info check: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html?hl=en#onBindViewHolder(VH,%20int)
edit:
as #Pawel has pointed out, the flags should be cleared when mTodo.isDone == 0.
that should get the job done