【发布时间】:2016-09-12 15:01:52
【问题描述】:
我在尝试为我的应用程序排序“路线”列表时遇到问题,无论我尝试什么,我都无法获得我正在寻找的排序。
我希望它按 1、2、3、4、5 等排序,但是当我排序时,我得到 1、11、12、2、20 等等。
我的路线模型是
public open class Route(docValue:Map<String,Any>) {
val route_id = (docValue["route_id"] as Number).toInt()
val short_name = docValue["route_short_name"] as String
val color = readColorMoreSafely(docValue, "route_color", Color.BLUE)
val long_name = docValue["route_long_name"] as String
}
用来排序的代码是
if(cityId != null && view != null) {
val routesList = view.findViewById(R.id.routesList) as ListView
val cache = TransitCache.getInstance(applicationContext, cityId, true)
val routes = cache.getRoutes()
.observeOn(AndroidSchedulers.mainThread())
.doOnNext {
val noRoutesMessage = view.findViewById(R.id.list_routes_no_routes_visible) as TextView
noRoutesMessage.visibility = if(it.size == 0) View.VISIBLE else View.GONE
}
routes.toSortedList()
listAdapter = RxListAdapter(applicationContext, R.layout.activity_list_routes_row, routes)
routesList.adapter = listAdapter
但还是什么都没有,我只想按“route_id”对路线进行排序,我尝试了一些不同的东西,最后一个是
routes.toSortedList()
这仍然没有做我想要的,在这一点上我被卡住了。
【问题讨论】:
-
请注意,Kotlin stdlib 中只读列表上的
routes.toSortedList()之类的函数会复制列表并返回一个新列表,因此您的变量routes并未实际更改为排序列表,而是您制作了一个副本并因为您没有分配它而将其丢弃。val sortedRoutes = routes.toSortedList()然后用sortedRoutes代替原来的列表。 -
@JaysonMinard 在 kotlin 标准库中没有这样的扩展函数 (
toSortedList)。是rx.Observable的成员函数 -
我一般说的是那种类型的函数。
toSortedList还返回一个新的Observable,必须使用它来代替原来的是吗?同样的问题,它不会就地排序原始文件,您必须使用生成的参考。