代码之家  ›  专栏  ›  技术社区  ›  Lechucico

对所有回收查看数据进行排序

  •  0
  • Lechucico  · 技术社区  · 7 年前

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

    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.array$ArrayList不能是

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

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   jguerinet    7 年前

    不用担心选角,用科特林的方法会简单得多 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 相反。但在这里 列表

    编辑-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)
    }
    

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