【问题标题】:how can I sort company list based on Name ascending, descending order in my adapter?如何根据适配器中的名称升序、降序对公司列表进行排序?
【发布时间】:2020-12-01 08:57:41
【问题描述】:

我正在开发新应用程序,如何根据适配器中的名称升序、降序对公司列表进行排序?

在我的适配器类下面

class SpectrumAdapter(
    private val spectrumResponse: ArrayList <SpectrumResponseItem>
) : RecyclerView.Adapter<SpectrumViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpectrumViewHolder {

        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.spectrum_list, parent, false)
        return SpectrumViewHolder(view)

    }

    override fun getItemCount(): Int {
        return spectrumResponse.size
    }

    override fun onBindViewHolder(holder: SpectrumViewHolder, position: Int) {
        return holder.bind(spectrumResponse[position])
    }
}

适配器类。

    class SpectrumViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val companyLogo: ImageView = itemView.findViewById(R.id.companyLogo)
        private val companyName: TextView = itemView.findViewById(R.id.companyName)
        private val companyWebsite: TextView = itemView.findViewById(R.id.companyWebsite)

        fun bind(spectrum: SpectrumResponseItem) {
            Glide.with(itemView.context).load(R.drawable.placehold).into(companyLogo)

            companyName.text = spectrum.company
            companyWebsite.text = spectrum.website
        }

    }

下面的 XML 布局列表

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <ImageView
            android:id="@+id/companyLogo"
            android:layout_width="45dp"
            android:layout_height="26dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="11dp"
            android:layout_marginRight="11dp"
            android:padding="4dp"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toTopOf="@+id/companyWebsite"
            app:layout_constraintEnd_toStartOf="@+id/companyName"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/companyWebsite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="56dp"
            android:layout_marginLeft="56dp"
            android:layout_marginTop="40dp"
            android:text="Company Website"
            android:textSize="8sp"
            app:layout_constraintBottom_toBottomOf="@id/companyLogo"
            app:layout_constraintStart_toStartOf="@+id/companyLogo"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/companyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="286dp"
            android:layout_marginRight="286dp"
            android:text="Company Name"
            android:textSize="8sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/companyLogo"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

我想实现排序功能,用户可以对公司名称进行升序和降序排序,我该如何实现?

【问题讨论】:

    标签: android sorting kotlin


    【解决方案1】:

    您可以在适配器中添加方法 sortAscending 和 sortDescending。但是您需要将private val spectrumResponse: ArrayList 更改为var,因为可以在排序时更改数据。这是适配器的完整代码示例。

    class SpectrumAdapter(
        private var spectrumResponse: ArrayList <SpectrumResponseItem>
    ) : RecyclerView.Adapter<SpectrumViewHolder>() {
    
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpectrumViewHolder {
    
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.spectrum_list, parent, false)
            return SpectrumViewHolder(view)
    
        }
    
        override fun getItemCount(): Int {
            return spectrumResponse.size
        }
    
        override fun onBindViewHolder(holder: SpectrumViewHolder, position: Int) {
            return holder.bind(spectrumResponse[position])
        }
    
        fun sortAscending() {
            spectrumResponse = spectrumResponse.sortedBy { it.name }
            notifyDataSetChanged()
        }
    
        fun sortDescending() {
            spectrumResponse = spectrumResponse.sortedByDescending { it.name }
            notifyDataSetChanged()
        }
     
    }
    

    然后你可以像这样在你的活动按钮中调用它。

    btn_sort_ascending.setOnClickListener {
        adapter.sortAscending()
    }
    

    btn_sort_ascending 是按钮的视图 ID。 adapter 是您创建的适配器。

    【讨论】:

    • 非常感谢您的回答,我可以问一个问题吗?主机recyclerview。
    • 非常感谢您的回答,我可以问一个问题吗?主机recyclerview。
    • 是否需要对多个activity实现不同的过滤功能?如果是这样,那么我建议在您的适配器中创建一个方法来过滤列表,然后在调用它时在参数中传递标准。我编辑我的答案以显示示例。
    • Najat,现在我只想实现过滤器,如果您需要我的 mainactivity 和 XML 我也可以放置,用户可以在其中对 companyName 进行升序和降序排序
    • 哦对不起,我以为你需要一些特殊的过滤器。如果你这么说,那么你需要为按钮添加监听器。单击按钮时,只需从适配器调用 sortAscending 函数。你可以这样称呼它btn_sort_ascending.setOnClickListener { adapter.sortAscending() }
    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 2018-05-03
    • 2016-10-18
    • 2021-08-23
    • 2020-11-09
    • 2020-09-27
    • 2021-08-24
    相关资源
    最近更新 更多