Я использую следующий адаптер для создания выпадающего меню Spinner. Мой адаптер отображает только один текст для каждого элемента. Этот пункт переведен. Этот код работает хорошо, но в конце счетчик содержит значения, которые не отсортированы в алфавитном порядке.
Как изменить код для сортировки данных и сохранения перевода?
Activity. kt:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
spinner.adapter = CarsDropdownAdapter(this, carsArray)
массив автомобилей. xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="units_array">
<item>CAR_LABEL1</item>
<item>CAR_LABEL2</item>
<item>CAR_LABEL3</item>
</string-array>
</resources>
CarsDropdownAdapter.kt:
class CarsDropdownAdapter(ctx: Context, private val cars: Array<String>) : ArrayAdapter<String>(ctx, 0, cars) {
override fun getView(position: Int, recycledView: View?, parent: ViewGroup): View {
return this.createView(position, recycledView, parent)
}
override fun getDropDownView(position: Int, recycledView: View?, parent: ViewGroup): View {
return this.createView(position, recycledView, parent)
}
private fun createView(position: Int, recycledView: View?, parent: ViewGroup): View {
val carLabel = getItem(position)
val view = recycledView ?: LayoutInflater.from(parent.context).inflate(R.layout.item_car_dropdown, parent, false)
// Used to get the translation but at the end the spinner contains Data that are not sorted alphabetically !
view.car_name_dropdown.text = context.getString(parent.context.resources.getIdentifier(carLabel, "string", context?.packageName))
return view
}
}