如何使用Kotlin在RecyclerView中搜索和过滤结果?

时间:2018-10-13 11:22:05

标签: android filter android-recyclerview kotlin

我想在不使用工具栏的情况下在我的应用程序中添加搜索工具,所以我尝试了这些代码,但没有给我所需的结果。我的真正问题是我不知道如何从使用的ArrayList中过滤结果一个带有for循环的EdiText,但无法正常工作,请帮助我。

  

图书清单

@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class List_of_Books : AppCompatActivity() {

    var booksList:MutableList<Book> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list_of__books)

        books_listView.layoutManager = GridLayoutManager(this,2)
        booksList.add(Book("He Died with...", "Categorie Book", "Description book", R.drawable.hediedwith))
        booksList.add(Book("The Vegitarian", "Categorie Book", "Description book", R.drawable.thevigitarian))
    val myrv = findViewById<RecyclerView>(R.id.books_listView)
    /* val myAdapter = RecyclerViewAdapter(this, lstBook)*/
    val myAdapter = RecyclerViewAdapter(this, booksList)

    myrv.layoutManager = GridLayoutManager(this, 3)
    /*  myrv.adapter = myAdapter*/
    myrv.adapter =myAdapter
  

Book.kt

class Book(title: String, category: String, description: String, var thumbnail: Int) {

    var title: String? = title
    var category: String? = category
    var description: String? = description

}
  

RecyclkerViewAdapter.kt

class RecyclerViewAdapter(private val mContext: List_of_Books, private val mData: List<Book>) : RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
    override fun getItemCount(): Int {
        return mData.size
    }


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

        val view: View
        val mInflater = LayoutInflater.from(mContext)
        view = mInflater.inflate(R.layout.cardveiw_item_book, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        holder.tv_book_title.text = mData[position].title
        holder.img_book_thumbnail.setImageResource(mData[position].thumbnail)
        holder.cardView.setOnClickListener {
            val intent = Intent(mContext, Book_Activity::class.java)

            // passing data to the book activity
            intent.putExtra("Title", mData[position].title)
            intent.putExtra("Description", mData[position].description)
            intent.putExtra("Thumbnail", mData[position].thumbnail)
            intent.putExtra("Category", mData[position].category)
            intent.putExtra("targetURL", "https://s1-physique.blogspot.com/")


            // start the activity
            mContext.startActivity(intent)
        }


    }

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

        internal var tv_book_title: TextView = itemView.findViewById(R.id.book_title_id) as TextView
        internal var img_book_thumbnail: ImageView = itemView.findViewById(R.id.book_img_id) as ImageView
        internal var cardView: CardView = itemView.findViewById(R.id.cardview_id) as CardView

    }


}

如果您还需要其他任何内容,请在评论中告诉我。

0 个答案:

没有答案