Android Kotlin:在适配器中对数据进行排序并保持翻译
Android Kotlin : Sort data in adapter and keeping translation
我正在使用以下适配器创建微调器下拉菜单。
我的适配器只为每个项目显示一个文本。此项目已翻译。
此代码运行良好,但最后微调器包含未按字母顺序排序的值。
如何修改代码以对数据进行排序并保留翻译?
Activity.kt:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
spinner.adapter = CarsDropdownAdapter(this, carsArray)
汽车-array.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
}
}
您可以在创建适配器之前对字符串数组进行排序:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
carsArray.sort()
spinner.adapter = CarsDropdownAdapter(this, carsArray)
字符串的 sort
实现按字母顺序对它们进行排序
使用以下代码:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
Collections.sort(carsArray)
spinner.adapter = CarsDropdownAdapter(this, carsArray)
第 Collections.sort(carsArray)
行将按字母顺序对数组进行排序。
我正在使用以下适配器创建微调器下拉菜单。 我的适配器只为每个项目显示一个文本。此项目已翻译。 此代码运行良好,但最后微调器包含未按字母顺序排序的值。
如何修改代码以对数据进行排序并保留翻译?
Activity.kt:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
spinner.adapter = CarsDropdownAdapter(this, carsArray)
汽车-array.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
}
}
您可以在创建适配器之前对字符串数组进行排序:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
carsArray.sort()
spinner.adapter = CarsDropdownAdapter(this, carsArray)
字符串的 sort
实现按字母顺序对它们进行排序
使用以下代码:
val spinner: Spinner = findViewById(R.id.units_cars)
val carsArray = resources.getStringArray(R.array.cars_array)
Collections.sort(carsArray)
spinner.adapter = CarsDropdownAdapter(this, carsArray)
第 Collections.sort(carsArray)
行将按字母顺序对数组进行排序。