I aim to make a simple to-do list and for that I have used RecyclerView. My issue is that the OnClickListener() in the RecyclerView adapter class is not getting called. I have no idea why this is happening.
P.S I am fairly new to Android Development so please excuse any stupid mistake I have made.
RecyclerView adapter class:
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_task.*
import kotlinx.android.synthetic.main.item_todo.view.*
import java.text.SimpleDateFormat
import java.util.*
class TodoAdapter(val list: List<TodoModel>) : RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
return TodoViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.item_todo, parent, false)
)
}
override fun getItemCount() = list.size
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.bind(list[position],position)
holder.itemView.setOnClickListener {
Log.e("dbg","click")
Toast.makeText(holder.itemView.context,"This should be called",Toast.LENGTH_SHORT).show()
}
}
override fun getItemId(position: Int): Long {
return list[position].id
}
class TodoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(todoModel: TodoModel,pos:Int) {
with(itemView) {
txtShowTitle.text = todoModel.title
txtShowTask.text = todoModel.description
txtShowCategory.text = todoModel.category
updateTime(todoModel.time)
updateDate(todoModel.date)
}
}
private fun updateTime(time: Long) {
//4:55 AM
val myformat = "h:mm a"
val sdf = SimpleDateFormat(myformat)
itemView.txtShowTime.text = sdf.format(Date(time))
}
private fun updateDate(time: Long) {
//Mon, 5 Jan 2020
val myformat = "EEE, d MMM yyyy"
val sdf = SimpleDateFormat(myformat)
itemView.txtShowDate.text = sdf.format(Date(time))
}
}
}
item xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/item"
app:cardBackgroundColor="#818181"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="10dp"
android:elevation="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:theme="@style/allDetailsTextColor">
<TextView
android:id="@+id/txtShowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginEnd="5dp"
android:layout_toStartOf="@+id/txtShowCategory"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
android:textStyle="bold"
tools:text="Task Title" />
<TextView
android:id="@+id/txtShowTask"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtShowTitle"
android:layout_alignParentStart="true"
android:layout_marginTop="4dp"
android:textSize="16sp"
tools:text="Task Subtitle" />
<TextView
android:id="@+id/txtShowCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@drawable/spinner_item_background"
android:padding="4dp"
android:ellipsize="end"
android:maxWidth="80dp"
android:maxLines="1"
android:textSize="12sp"
tools:text="Category" />
<TextView
android:id="@+id/txtShowDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtShowTask"
android:layout_marginTop="30dp"
android:layout_toLeftOf="@+id/txtShowTime"
android:layout_marginRight="10dp"
android:visibility="visible"
tools:text="22-12-2017" />
<TextView
android:id="@+id/txtShowTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtShowTask"
android:layout_alignParentEnd="true"
android:layout_marginTop="30dp"
android:fontFamily="@font/montserrat"
android:visibility="visible"
tools:text="22:12 AM" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Any help would be appreciated.