在Kotlin中,ArrayAdapter需要(Context context,int resource)作为参数,但我想在ArrayList中作为字符串如何才能做到这一点?

问题描述 投票:-1回答:2
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??)
android arraylist kotlin android-arrayadapter
2个回答
1
投票

如果你只想要一个带有ListViewArrayAdapter,这样的东西可能会让你开始:

    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

0
投票

把第三个参数放入你的WordAdapter

class WordAdapter(context: Context, @LayoutRes resource: Int, private val wordList: ArrayList<Word>) : ArrayAdapter<Word>(context, resource)
© www.soinside.com 2019 - 2024. All rights reserved.