排序所有RecyclerView数据

时间:2019-03-13 15:28:23

标签: android arraylist android-recyclerview

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

在主要活动中,我使用以下方法:

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
}

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

1 个答案:

答案 0 :(得分: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)
}

这样,您无需每次都创建一个新适配器,只需使用相同的适配器即可。

相关问题