繁体   English   中英

在Kotlin ArrayAdapter中需要(上下文上下文,int资源)作为参数,但是我想要作为ArrayList中的字符串,h怎么做?

[英]in Kotlin ArrayAdapter need (Context context,int resource) as parameter but i want as a string in the ArrayList how h can do that?

class Word(var mDefultTranslation: String, var mArabicTranslation: String ) {
      fun Word (defultTranslation : String , arabicTranslation : String  ){
          mDefultTranslation = defultTranslation
          mArabicTranslation = arabicTranslation
      } 
}

val words = arrayListOf<Word>()
words.add( Word("one","two" ))  
val wordAdapter = WordAdapter (this, word??)

如果您只想使用带有ArrayAdapterListView ,则可以使用以下方法入门:

    val adapter = object : ArrayAdapter<Word>(this, android.R.layout.simple_list_item_2, android.R.id.text1, words) {
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view = super.getView(position, convertView, parent)
            val text1 = view.findViewById(android.R.id.text1) as TextView
            val text2 = view.findViewById(android.R.id.text2) as TextView
            text1.setText(words[position].mDefultTranslation)
            text2.setText(words[position].mArabicTranslation)
            return view
        }
    }
    listview.adapter = adapter

将第三个参数放入您的WordAdapter

class WordAdapter(context: Context, @LayoutRes resource: Int, private val wordList: ArrayList<Word>) : ArrayAdapter<Word>(context, resource)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM