【问题标题】:Sort all RecyclerView data对所有 RecyclerView 数据进行排序
【发布时间】:2019-03-13 15:28:22
【问题描述】:

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

在主要活动中,我使用这个:

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

我在方法 onCreate 上创建适配器:

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

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

【问题讨论】:

  • 你能发布你初始化items元素的方式吗?
  • 更新了项目初始化
  • 您使用 ArrayList 而不仅仅是 Kotlin MutableList 是否有特定原因?这样你就不用担心强制转换了,让 Kotlin 处理其MutableList 的实际实现。
  • 与 MutableList 完美结合。

标签: android arraylist android-recyclerview


【解决方案1】:

不用担心强制转换,直接在这里使用 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)
}

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

【讨论】:

  • 一切正常,但viewAdapter.notifyDataSetChanged() 不会更新回收站视图列表。我必须使用新的 sortedList 创建一个新的 viewAdapter,并将适配器重新分配给 recyclerView。
  • 如果你添加你的适配器创建代码我可以看看。
  • 编辑了我的答案。我猜测了SimpleItemRecyclerViewAdapter 的外观,如果它看起来与我输入的不相似,只需将该代码添加到问题中,我会修正我的答案。但我相信这足以让您了解我在做什么!
猜你喜欢
  • 2015-12-10
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多