当前位置: 首页 > 知识库问答 >
问题:

未解析的引用:切换到视图绑定后的myViewHolder

齐起运
2023-03-14

在从删除kotlin_extensions切换到视图绑定之后,我在onBindViewHolder方法中收到了一个“Unresolded reference:MyViewholder”,当我将“MyViewholder”替换为“holder”时,它会给我一个“Unresolded reference:bind”。我怎么解决这个问题。

MyAdapter

class MyAdapter(private val context: Context, private val mHelper : TaskDbHelper) : RecyclerView.Adapter<MyAdapter.MyViewHolder>(),
SwipeAndDragHelper.ActionCompletionContract {
class MyViewHolder(val binding: CellCardsBinding): RecyclerView.ViewHolder(binding.root ) {
    fun binding() {

    }
}
private var touchHelper: ItemTouchHelper? = null
private var list = mutableListOf<MyObject>()

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
    initList()
    super.onAttachedToRecyclerView(recyclerView)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
   return MyViewHolder(CellCardsBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}

@RequiresApi(Build.VERSION_CODES.P)
@SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    val myObject = list[position]

   myViewHolder.bind(myObject)

    val activity: Activity = context as Activity
    
    holder.binding.text.setOnClickListener{
        activity.launchActivity<AddNoteActivity>(42) {
            putExtra("PositionInList", position.toString())
            putExtra("TaskTitle", myObject.title)
            putExtra("TaskText", myObject.text)
        }
    }

    activity.findViewById<RecyclerView>(R.id.recyclerView).setOnTouchListener { _, event ->
        when (event.actionMasked) {
            MotionEvent.ACTION_UP -> {
                updateNotesPositionInDb()
                false
            }

            else -> {
                false
            }
        }
    }

    holder.binding.title.setOnTouchListener { _, event ->
        when (event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                touchHelper!!.startDrag(holder)
                false
            }
            else -> {
                false
            }
        }

    }
}

private fun initList() {
    list.clear()
    val db = mHelper.readableDatabase
    val cursor = db.query(
        TaskContract.TaskEntry.TABLE,
        arrayOf(
            TaskContract.TaskEntry.ID,
            TaskContract.TaskEntry.COL_TASK_TITLE,
            TaskContract.TaskEntry.COL_TASK_TEXT,
            TaskContract.TaskEntry.COL_TASK_DATE),null, null, null, null, TaskContract.TaskEntry.ID)
    while (cursor.moveToNext()) {
        val id = cursor.getColumnIndex(TaskContract.TaskEntry.ID)
        val idTitle = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE)
        val idText = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TEXT)
        val idDate = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_DATE)

        list.add(MyObject(cursor.getString(id), cursor.getString(idTitle), cursor.getString(idText), cursor.getString(idDate)))
    }
    notifyDataSetChanged()

    cursor.close()
    db.close()
}

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

override fun onViewMoved(oldPosition: Int, newPosition: Int) {
    val target = list[oldPosition]
    list.removeAt(oldPosition)
    list.add(newPosition, target)
    notifyItemMoved(oldPosition, newPosition)
}

override fun onViewSwiped(position: Int) {
    deleteTask(list[position].ID)
    list.removeAt(position)
    notifyItemRemoved(position)
    updateNotesPositionInDb()
}
fun setTouchHelper(touchHelper: ItemTouchHelper) {
    this.touchHelper = touchHelper
}

fun addTask(taskTitle : String, taskText: String) {
    val values = ContentValues()

    val sdf = SimpleDateFormat("dd/MM/yyyy/", Locale.US)
    val date = sdf.format(Date())

    values.put(TaskContract.TaskEntry.ID, list.size)
    values.put(TaskContract.TaskEntry.COL_TASK_TITLE, taskTitle)
    values.put(TaskContract.TaskEntry.COL_TASK_TEXT, taskText)
    values.put(TaskContract.TaskEntry.COL_TASK_DATE, date)

    val db = mHelper.readableDatabase
    db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
        null,
        values,
        SQLiteDatabase.CONFLICT_REPLACE)
    db.close()

    list.add(MyObject(list.size.toString(), taskTitle, taskText, date))

    notifyItemInserted(list.size)
}

fun addTask() {
    val test: Activity = context as Activity
    test.launchActivity<AddNoteActivity>(42) {
        /* putExtra("user", "854")
         p utExtra("user2", "46850") */
    }
}

private fun deleteTask(taskId: String) {

    val db = mHelper.readableDatabase
    db.delete(TaskContract.TaskEntry.TABLE,
        "id=$taskId", null)
    db.close()
}

fun modifyTask(taskPosition: String, taskTitle: String, taskText: String) {
    val target = list[taskPosition.toInt()]

    target.title = taskTitle
    target.text = taskText

    val values = ContentValues()

    val sdf = SimpleDateFormat("dd/MM/yyyy/", Locale.US)
    val date = sdf.format(Date())

    values.put(TaskContract.TaskEntry.ID, taskPosition)
    values.put(TaskContract.TaskEntry.COL_TASK_TITLE, taskTitle)
    values.put(TaskContract.TaskEntry.COL_TASK_TEXT, taskText)
    values.put(TaskContract.TaskEntry.COL_TASK_DATE, date)

    val db = mHelper.readableDatabase
    db.update(TaskContract.TaskEntry.TABLE,
        values, TaskContract.TaskEntry.ID + "=" + target.ID, null)

    db.close()

    notifyItemChanged(taskPosition.toInt())
}

private fun updateNotesPositionInDb() {
    val db = mHelper.readableDatabase

    var i = 0
    while (i < list.size) {
        val values = ContentValues()

        values.put(TaskContract.TaskEntry.ID, i)
        db.update(TaskContract.TaskEntry.TABLE,
            values, TaskContract.TaskEntry.ID + "=? AND " + TaskContract.TaskEntry.COL_TASK_TITLE + "=?", arrayOf(list[i].ID, list[i].title))
        i++

    }
    db.close()
}

我试着阅读Android Studio的官方文档,但它不能解决我的特定问题。

共有1个答案

裴欣荣
2023-03-14

在您的类MyViewHolder中,您有一个名为binding的方法,您还需要实现它并添加paramter shoud be

class MyViewHolder(private val binding: CellCardsBinding): RecyclerView.ViewHolder(binding.root ) {
    fun bind(data:MyObject) {
        binding.yourView=data.title ...
    }
}

在onBindViewHolder中

..
holder.bind(myObject)

 类似资料:
  • 我一直得到错误"未解决的引用:绑定"。有人知道我错过了什么吗?我已经试着更新了一切。但是绑定不起作用。 建筑格拉德尔。(module.app):

  • 我使用新的数据绑定库在Java编写了以下片段类 它的编译和运行都很好。 我尝试用Kotlin重写它,并得出以下结论: 但现在步骤输出以下内容: 错误:(16,38)未解析引用:DataBinding 错误:(37,27)未解析引用:FragmentDataBinding 我如何使用android-databinding库与Kotlin? 我的顶级: 应用程序目录中的我的(仅相关部分): 我使用的是

  • 我使用Android Studio Beta 1与和。我还启用了数据绑定: 但不幸的是,Kotlin类看不到从布局生成的类,因为我会遇到如下错误: 错误:(17,31)未解析的引用:数据绑定 错误:(39,36)未解决的引用:MyFragment绑定 当然,Java类可以看到这些生成的类。

  • 我正在将一个kotlin库绑定到xamarin android,绑定并解决所有错误后,我得到了一个意外错误,没有任何解释或异常原因, JAVALang.NoClassDefFoundError:“未能解析:Landroidx/viewbinding/viewbinding;” 这是我得到的堆栈痕迹: MAYActivity.cs:40在Android。App.活动。n_OnCreate_Landr

  • 我正在尝试在我的代码上调用DrawImage(),我正在遵循这个教程Jetack教程, 但是我收到了这个错误。: 未解析的引用:绘制图像 我尝试在构建时实现。gradle,此库:实现“androidx.ui:ui基础:0.1.0-dev04” 但是我得到了这个错误。: 在项目C:\Users\samoe\Desktop\Projetos Kotlin\first-jetpack中执行任务:[cle

  • 我最近从Indigo升级到了Juno,虽然到目前为止似乎一切正常,但每次切换透视图时都有一个例外: 发生错误。有关详细信息,请参阅错误日志。org.eclipse.ui.internal.workbenchpage.findPerspective(log/eclipse/ui/iperspectiveDescriptor;)log/eclipse/ui/internal/perspective;