Я хочу добавить инструмент поиска в свое приложение без использования панели инструментов, поэтому я попробовал эти коды, но это не дает мне требуемого результата. Моя настоящая проблема в том, что я не знал, как отфильтровать результат из моего ArrayList, который я использовалEdiText с циклами for, но он не работает правильно. Пожалуйста, помогите мне.
List_of_Books
@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
}
}
Если вам нужно что-то еще, просто скажите мне в комментариях.