无法在android studio中调用variables
在这里输入图像描述我想做一个简单的调用我已经在Android应用程序中的布局资源如下。 我所需要做的就是能够使用像myInteger这样的variables(具有诸如1,2,3的值)并且调用R.layot
来自土耳其的Hassan兄弟你好。用这个改变你的代码好吗?
class AnmailAdapter(val context: Context, val listOfAnmail: ArrayList ): 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(R.id.tvName) val tvDesc = view.findViewById (R.id.tvDes) val imgView = view.findViewById(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(R.id.tvName)
代替;
val tvName = view.findViewById(R.id.tvName) as TextView
尝试生成>重建项目或生成>清洁项目在你的android工作室。
尝试使用(myView.findViewById(R.id....) as TextView).text = animal.name!!
或者,如果您使用kotlin-android-extensions
检查此类中的导入。 如果您从导入的不同布局查看重复的ID,则不能使用扩展