在 Kotlin ArrayAdapter 中需要 (Context context,int resource) 作为参数,但我想作为 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??)
如果您只想要 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)
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)