所需类型与找到的类型不同

时间:2019-05-18 18:18:17

标签: android kotlin android-recyclerview

我正在尝试使用该ArrayList构建Alarm类,ALarm的ArrayList和RecyclerView

已经搜索到任何无关紧要的地方

    override fun onBindViewHolder(holder: RecyclerAdapter.AlarmHolder, position: Int) {
        val itemAlarm = alarmlist.get(position)
            holder.onClick(itemAlarm)
    }
在必要时holder.onClick(itemAlarm)发现Alarm的错误View。看到我今天遇到了这个问题,仍然无法解决,现在是01 30 am,所以我已经尽力了。 有人可以解释我如何解决这个问题吗? 感谢您对这个问题的详细答复:)

在下面完成RecyclerAdapter类

package com.example.alarm

import android.app.TimePickerDialog
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.TimePicker
import android.widget.Toast
import org.w3c.dom.Text
import java.util.*
import kotlin.collections.ArrayList

class RecyclerAdapter(private val alarmlist: ArrayList<Alarm>) :
    RecyclerView.Adapter<RecyclerAdapter.AlarmHolder>()  {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.AlarmHolder {
        val inflatedView = parent.inflate(R.layout.recycler_item_row, false)
        return AlarmHolder(inflatedView)
    }

    override fun getItemCount(): Int {
        return alarmlist.size
    }

    override fun onBindViewHolder(holder: RecyclerAdapter.AlarmHolder, position: Int) {
        val itemAlarm = alarmlist.get(position)
            holder.onClick(itemAlarm)
    }

    //1
    class AlarmHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
        //2
        private var view: View = v
        private lateinit var context: Context

        // redundant
        init {
            v.setOnClickListener(this)
        }

        //4
        override fun onClick(v: View) {
            /*Code below
            * is used to pick Time*/
            fun clickTimePicker(view: View) {
                val c = Calendar.getInstance()
                val hour = c.get(Calendar.HOUR)
                val minute = c.get(Calendar.MINUTE)
                /* Context may generate error
                * context not inisitalized*/
                var itemtitle: TextView ?= null


                val tpd = TimePickerDialog(context, TimePickerDialog.OnTimeSetListener(function =  {
                        view, h, m ->
                    Toast.makeText(context, h.toString() + " : " + m + " : ", Toast.LENGTH_LONG).show()
                    /*Passing value of Hour
                     minute
                     to textView*/
                    itemtitle?.setText(h.toString() + " " + m.toString())

                }),hour,minute,false)

                tpd.show()
            }
        }

        companion object {
            //5
            private val PHOTO_KEY = "PHOTO"
        }
    }
}

警报类别

package com.example.alarm

import android.widget.TimePicker

data class Alarm(val timepicker: TimePicker) {


}

1 个答案:

答案 0 :(得分:0)

为什么要调用onClick()?是错误!在onBindViewHolder()中,应将视图与itemAlarm模型绑定。

也许您想要setOnClickListenter(yourClickListener)?