Kotlin ArrayAdapter Ошибка: ни одна из следующих функций не может быть вызвана с предоставленными аргументами - PullRequest
0 голосов
/ 23 октября 2018

Я новичок в Kotlin для разработки под Android.Следуя учебному проекту, мне нужно было использовать ArrayAdapter с пользовательским классом.Сборка проекта завершилась ошибкой.

Класс MainActivity.kt выдает ошибку:

    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    }

    val dm = DataManager()
    val adapterCourses = ArrayAdapter<CourseInfo>(
        context = this,
        android.R.layout.simple_spinner_item,
        dm.courses.values.toList()
    )

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}

Ошибка:

None of the following functions can be called with the arguments supplied:
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, @RecentlyNonNull p2: Array<(out) CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int, @RecentlyNonNull p3: Array<(out) CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int, @RecentlyNonNull p3: (Mutable)List<CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, @RecentlyNonNull p2: (Mutable)List<CourseInfo!>!) defined in android.widget.ArrayAdapter

Класс CourseInfo:

class CourseInfo (val courseId: String, val title: String)

Класс DataManager:

class DataManager {
    val courses = HashMap<String, CourseInfo>()
    val notes = ArrayList<NoteInfo>()

    init {
        initializeCourses()
    }

    private fun initializeCourses () {
        var course = CourseInfo(courseId = "android_intent", title = "Android programming with intent.")
        this.courses.set(course.courseId, course)

        course = CourseInfo(courseId = "android_async", title = "Android Async Programming and Services.")
        courses.set(course.courseId, course)

        course = CourseInfo(courseId = "java_lang", title = "Java Fundamentals: The Java Language.")
        courses.set(course.courseId, course)

        course = CourseInfo(courseId = "java_core", title = "Java Fundamentals: The Core Platforms.")
        courses.set(course.courseId, course)
    }
}

Ответы [ 2 ]

0 голосов
/ 23 октября 2018

Именованные аргументы недопустимы для не-kotlin-функций.

Проблема в том, что вы вызываете java ArrayAdapter конструктор в kotlinфайл.вы не сможете предоставить ему метку аргумента.

, так что после простого исправления это решит проблему (обратите внимание, здесь я удалил метку контекста в первом аргументе.)

val adapterCourses = ArrayAdapter<CourseInfo>(this,
            android.R.layout.simple_spinner_item,
            dm.courses.values.toList())
0 голосов
/ 23 октября 2018

Используйте ниже код

val courseList:List<CourseInfo>= dm.courses.values.toList()
val adapterCourses = ArrayAdapter<CourseInfo>(this,android.R.layout.simple_spinner_item,courseList)
...