对所有RecyClerview数据进行排序



我想使用anandroid菜单选项对所有回收器视图数据进行排序。

在主要活动中,我使用此:

companion object
{
    var items = ArrayList<BookContent.BookItem>()
    init
    {
        items.add(BookContent.BookItem(0, "Title 2", "Albert", Date(), "Description 2", ""))
        items.add(BookContent.BookItem(1, "Title 4", "Joan", Date(), "Description 4", ""))
        items.add(BookContent.BookItem(0, "Title 1", "Laura", Date(), "Description 1", ""))
        items.add(BookContent.BookItem(1, "Title 15", "Valeria", Date(), "Description 15", ""))
        items.add(BookContent.BookItem(0, "Title 6", "Jensen", Date(), "Description 6", ""))
        items.add(BookContent.BookItem(1, "Title 27", "Fran", Date(), "Description 27", ""))
        items.add(BookContent.BookItem(0, "Title 32", "Tim", Date(), "Description 32", ""))
        items.add(BookContent.BookItem(1, "Title 3", "James", Date(), "Description 3", ""))
        items.add(BookContent.BookItem(0, "Title 8", "Marie", Date(), "Description 8", ""))
        items.add(BookContent.BookItem(1, "Title 9", "Marta", Date(), "Description 9", ""))
        items.add(BookContent.BookItem(0, "Title 44", "Eli", Date(), "Description 44", ""))
    }
}
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
    items = when (item.itemId)
    {
        R.id.sort_author -> items.sortedWith(compareBy({it.author},{it.author})) as ArrayList<BookContent.BookItem>
        R.id.sort_title -> items.sortedWith(compareBy({it.title},{it.title})) as ArrayList<BookContent.BookItem>
        else -> ArrayList<BookContent.BookItem>()
    }
    viewAdapter.notifyDataSetChanged()
    return super.onOptionsItemSelected(item)
}

,但我获得以下错误:

java.lang.classcastException:java.util.arrays $ arraylist不能为 施放到java.util.arraylist

我在方法上创建适配器:

viewManager = LinearLayoutManager(this)
viewAdapter = SimpleItemRecyclerViewAdapter(items)
recyclerView = findViewById<RecyclerView>(R.id.book_recycler_view).apply {
    layoutManager = viewManager
    adapter = viewAdapter
}

否则,这是更新数据的正确方法吗?

而不是担心铸造,直接在这里使用Kotlin的List要简单得多,让Kotlin担心它使用的基础类。您可以按以下方式执行此操作:

companion object
{
    // Note: I've also combined the list declaration and instantiation into one here
    var items = listOf(
        BookContent.BookItem(0, "Title 2", "Albert", Date(), "Description 2", ""), 
        ...
    )
}
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
    items = when (item.itemId)
    {
        // Note: I use sortedBy() instead or sortedWith() here to avoid needing to create 
        //  a Comparator, and because you're comparing the same property on each object. 
        //  I haven't tested this, but I'm fairly certain this would work the same as what you have
        R.id.sort_author -> items.sortedBy { it.author }
        R.id.sort_title -> items.sortedBy { it.title }
        else -> listOf()
    }
    viewAdapter.notifyDataSetChanged()
    return super.onOptionsItemSelected(item)
}

如果您需要它是可变列表(例如ArrayList),则可以改用MutableList。但是在这里List可以完成这项工作。

编辑 - 2019年3月15日

为了使用相同的适配器而不是每次需要更新列表时创建新的适配器,只需在适配器中添加update()方法,该方法设置其内部项目列表。我不知道您的适配器是什么样的,但是我认为您可以将其修改为看起来像这样:

class SimpleItemRecyclerViewAdapter(private var items: List<BookContent.BookItem>) {
    ...
    fun update(newItems: List<BookContent.BookItem>) {
        // Update the list of items used by the adapter
        items = newItems
        notifyDataSetChanged()
    }
}

然后,在您的活动中:

override fun onOptionsItemSelected(item: MenuItem): Boolean) {
    ...
    viewAdapter.update(items)
    return super.onOptionsItemSelected(item)
}

这样,您不需要每次创建一个新的适配器,只需使用相同的适配器。

最新更新