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??)
如果你只想要一个带有ListView
的ArrayAdapter
,这样的东西可能会让你开始:
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)