类型不匹配。必需:ContentResolver!找到:整数

时间:2018-11-10 15:10:30

标签: android kotlin

我想在我的应用程序中使用列表视图适应来更改单词的含义l想要在我的应用程序中使用字符串,lang英语和阿拉伯语向语言添加语言l要在我的列表适应中使用这些资源,因为我在我的应用

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val view : View = LayoutInflater.from(context).inflate(R.layout.row_layout,parent,false)

    val code = view.findViewById(R.id.code_id) as AppCompatTextView



    code.text = list[position].code

    if (code.equals("scheduled")) {
        getString(R.string.scheduled).toString(code)
    }




    return view as View
}

我遇到了错误

Type mismatch.
Required:
ContentResolver!
Found:
Int

2 个答案:

答案 0 :(得分:0)

他的工作方式很好

  if (code.text == ("scheduled")) {
        code.setText(R.string.scheduled)
    }

答案 1 :(得分:0)

部分问题是您的命名方式导致混乱。

您可能在if (code.equals("scheduled"))中尝试检查列表项的code属性的值。相反,您正在将code [AppCompatText] View与字符串进行比较。

这是一种实现方式。

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val view: View = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false)

    val codeView = view.findViewById<AppCompatTextView>(R.id.code_id)


    getItem(position)?.run {
        codeView.text = when (code) {
            "scheduled" -> context.getString(R.string.scheduled_text)
            else -> code
        }
    }
    return view
}

这也是今天第二次我看到有人在getView中膨胀一个布局。请阅读以下内容:

https://developer.android.com/training/improving-layouts/smooth-scrolling#ViewHolder

我已经扩展了example I put up on GitHub(以回答另一个问题),以便它也可以展示上下文翻译。