0

I have this code for my adapter.

class AdapterPendingCases(private val values: MutableList<String>):RecyclerView.Adapter<AdapterPendingCases.PendingCasesViewHolder>() {
    override fun getItemCount()=values.size

     inner class PendingCasesViewHolder constructor(itemView: View):RecyclerView.ViewHolder(itemView) {
         private val briefDescription=itemView.findViewById<TextView>(R.id.briefDescription)
         private val currData=itemView.findViewById<TextView>(R.id.currData)
         val deleteItem=itemView.findViewById<ImageView>(R.id.deleteItem) //надо как-то переделать с помощью ButterKnife

         fun bind(item:String) {
            briefDescription.text=item
             val date = getCurrentDateTime()
             val dateInString = date.toString("yyyy/MM/dd HH:mm:ss")
             currData.text= dateInString
        }

         private fun Date.toString(format: String, locale: Locale = Locale.getDefault()): String {
             val formatter = SimpleDateFormat(format, locale)
             return formatter.format(this)
         }

         private fun getCurrentDateTime(): Date {
             return Calendar.getInstance().time
         }

        fun deleteClick(View: View) {
            values.removeAt(adapterPosition)
            notifyDataSetChanged()
        }

     }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PendingCasesViewHolder{
        return PendingCasesViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_pending_cases, parent, false))}

    override fun onBindViewHolder(holder: PendingCasesViewHolder, position: Int) {
        holder.bind(values[position])
    }


    fun delAndUpdate(position: Int) {
        values.removeAt(position)
        notifyDataSetChanged()
    }

i want to use Butterknife and use @OnClick for deleteItem. Now i can't link a click for my deleteItem. I am ready to listen to other recommendations on the code. p.s. in the project Butterknife work correctly.

1 Answer 1

0

ButterKnife has a function that allows you to pass view and target it should be bound to: ButterKnife.bind(this, view). Thus, you can add all required annotations and use this function to get the expected behaviour. It works with any class. Target class doesn't have to be based on Fragment, Activity or any Android-specific class.

Here is a solution using ButterKnife annotations:

    inner class PendingCasesViewHolder : RecyclerView.ViewHolder {
        @BindView(R.id.briefDescription)
        lateinit var briefDescription: TextView

        @BindView(R.id.currData)
        lateinit var currData: TextView

        @BindView(R.id.deleteItem)
        lateinit var deleteItem: ImageView

        constructor(itemView: View) : super(itemView) {
            ButterKnife.bind(this, view)
        }

        ...

        @OnClick(R.id.deleteItem)
        fun deleteClick(view: View) {
            values.removeAt(adapterPosition)
            notifyDataSetChanged()
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.