【问题标题】:in Kotlin ArrayAdapter need (Context context,int resource) as parameter but i want as a string in the ArrayList how h can do that?在 Kotlin ArrayAdapter 中需要 (Context context,int resource) 作为参数,但我想作为 ArrayList 中的字符串 h 怎么能做到这一点?
【发布时间】:2018-03-04 03:59:41
【问题描述】:
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


    【解决方案1】:

    如果你只想要一个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
    

    【讨论】:

    【解决方案2】:

    将第三个参数放入您的WordAdapter

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多