如何在Kotlin中按降序过滤列表数据?

问题描述 投票:0回答:1

从Android应用程序中,我写了我想添加的内容,在工具栏中添加一个按钮,用作切换按钮。禁用切换(默认状态)时,应显示所有帖子,启用切换(轻按后)时,列表应仅显示将user_id设置为1并按降序对published_at进行排序的帖子。再次点击按钮将使其返回默认状态。请注意,服务器的postList返回日期的publishedAt和postList的publishedAt和user_id我想知道如何实现上述要求,我应该遵循哪种步骤

在MainActivity.kt中我的逻辑实现下面

class MainActivity:AppCompatActivity(){

@Inject
lateinit var restInterface: RestInterface


private fun initializeDagger() = App.appComponent.inject(this)

var context: Context? = null
private var filteredList: List<Post>? = null



private var recyclerView: RecyclerView? = null
private var switch1: Switch? = null
private var restAdapter: RestAdapter? = null

private var postList: List<Post>? = null
private var restList: RestList? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    initializeDagger()
    recyclerView = findViewById(R.id.recycler_view)
    switch1 = findViewById(R.id.switch1)
    fetchPosts()

    switch1.setOnclickListener {

        postList.forEach { postItem: Post ->
            if (postItem.userId == 1)
                filteredList.add(postItem)
        }

        recyclerView.post = filteredList
        recyclerView.notifyDatasetChanged()

    }
    // Collections.sort( filteredList.get(4).publishedAt, Collections.reverseOrder());
}





private fun fetchPosts() {
    val progress = ProgressDialog(this)
    progress.setMessage("Loading... ")
    progress.isIndeterminate = true
    progress.show()
    restInterface?.getPosts?.subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(object : DisposableSingleObserver<Response<RestList>>() {
            override fun onSuccess(response: Response<RestList>) {
                restList = response.body()

                val layoutManager = LinearLayoutManager(applicationContext)
                recyclerView?.layoutManager = layoutManager

                // initialize postList with posts
                postList = restList?.posts
                restAdapter = postList?.let { RestAdapter(it, restList) }
                recyclerView?.adapter = restAdapter
            }

            override fun onError(e: Throwable) {
                progress.dismiss()
                Toast.makeText(context, "" + e.message, Toast.LENGTH_SHORT).show()
            }
        })

}

}

在我的RestAdapter.kt下面

class RestAdapter(val post: List<Post>,val restList: RestList?) : RecyclerView.Adapter<RestAdapter.PostHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.post_list, null)
        return PostHolder(itemView)
    }

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


    override fun onBindViewHolder(holder: PostHolder, position: Int) {
        val posts = post[position]
        Picasso
            .get() // give it the context
            .load(posts.image) // load the image
            .into(holder.postImage)
        holder.userId.text = posts.userId.toString()
        holder.postTitle.text = posts.title
        holder.postTime.text = posts.publishedAt
        holder.postDescription.text = posts.description


    }

    class PostHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val postImage: ImageView = itemView.findViewById(R.id.postImage)
        val userId: TextView = itemView.findViewById(R.id.userId)
        val postTitle: TextView = itemView.findViewById(R.id.postTitle)
        val postTime: TextView = itemView.findViewById(R.id.postTime)
        val postDescription: TextView = itemView.findViewById(R.id.postDescription)

    }

}
android kotlin filter togglebutton
1个回答
0
投票

您可以在列表中使用.sortedByDescending或.sortedBy方法:)

© www.soinside.com 2019 - 2024. All rights reserved.