无法在 android 工作室中调用变量
cant call variable in android studio
enter image description here我想对 Android 应用程序中已有的布局资源进行简单调用,如下所示。我需要做的就是能够使用诸如 myInteger 的变量(具有诸如 1,2,3 的值)并调用 R.layot
在您的 android 工作室中尝试 Build>Rebuild Project 或 Build>Clean Project。
尝试使用(myView.findViewById(R.id....) as TextView).text = animal.name!!
或者如果您使用 kotlin-android-extensions
检查此 class 中的导入。如果您有来自不同布局的重复 ID 视图,您也不能使用扩展
哈桑兄弟你好Turkey.Change你的代码用这个好吗?
class AnmailAdapter(val context: Context, val listOfAnmail: ArrayList<Anmail> ): BaseAdapter() {
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val anmail = listOfAnmail[p0]
val view = LayoutInflater.from(context).inflate(R.layout.activity, p2, false)
val tvName = view.findViewById<TextView>(R.id.tvName)
val tvDesc = view.findViewById<TextView>(R.id.tvDes)
val imgView = view.findViewById<ImageView>(R.id.imgView)
tvName.text = anmail.name
tvDesc.text = anmail.des
imgView.setImageResource(anmail.image!!)
return view
}
override fun getItem(p0: Int): Any {
return listOfAnmail[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return listOfAnmail.size
}
}
如果你的kotlin版本比我低;
val tvName = view.findViewById<TextView>(R.id.tvName)
相反;
val tvName = view.findViewById(R.id.tvName) as TextView
enter image description here我想对 Android 应用程序中已有的布局资源进行简单调用,如下所示。我需要做的就是能够使用诸如 myInteger 的变量(具有诸如 1,2,3 的值)并调用 R.layot
在您的 android 工作室中尝试 Build>Rebuild Project 或 Build>Clean Project。
尝试使用(myView.findViewById(R.id....) as TextView).text = animal.name!!
或者如果您使用 kotlin-android-extensions
检查此 class 中的导入。如果您有来自不同布局的重复 ID 视图,您也不能使用扩展
哈桑兄弟你好Turkey.Change你的代码用这个好吗?
class AnmailAdapter(val context: Context, val listOfAnmail: ArrayList<Anmail> ): BaseAdapter() {
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val anmail = listOfAnmail[p0]
val view = LayoutInflater.from(context).inflate(R.layout.activity, p2, false)
val tvName = view.findViewById<TextView>(R.id.tvName)
val tvDesc = view.findViewById<TextView>(R.id.tvDes)
val imgView = view.findViewById<ImageView>(R.id.imgView)
tvName.text = anmail.name
tvDesc.text = anmail.des
imgView.setImageResource(anmail.image!!)
return view
}
override fun getItem(p0: Int): Any {
return listOfAnmail[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return listOfAnmail.size
}
}
如果你的kotlin版本比我低;
val tvName = view.findViewById<TextView>(R.id.tvName)
相反;
val tvName = view.findViewById(R.id.tvName) as TextView