#Day39 of #100DaysOfCode
Today I went to the city and enjoyed St.Patricks Day Parade…
It was absolute fun and equally tiring…
Some Highlights:
I finally finished the remaining puzzle pieces of the BookLog Application and now it displays the list of books and authors.
In continuation from the Realm Bytes: BookLog Topic.
the code for retrieving the booklist in the booklist fragment is all below:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listBinding?.rvBookList?.layoutManager = LinearLayoutManager(context)
setUpRecyclerView()
}
private fun setUpRecyclerView() {
adapter = BookListAdapter(realmList.where(BookRealm::class.java).sort("name").findAll())
listBinding?.rvBookList?.adapter = adapter
}
The Recycler View is used to display the list, the setup is called once the activity view is created, otherwise it throws error. Realm provides Realm Adapter to use with Recycler View to display the list.
The Recycler Adapter code is as follows:
class BookListAdapter(books: OrderedRealmCollection<BookRealm>): RealmRecyclerViewAdapter<BookRealm, BookListHolder>(books, true) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookListHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.booklist_item_layout, parent, false)
return BookListHolder(view)
}
override fun onBindViewHolder(holder: BookListHolder, position: Int) {
val book = getItem(position)
holder.bindValues(book)
}
}
The Holder code is as below:
class BookListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val bookName = itemView.bookName
private val authorName = itemView.authorName
val stringBuilder = StringBuilder("")
fun bindValues(book: BookRealm?){
bookName.text = book?.name
book?.authors?.forEach{
stringBuilder.append(it.name).append(" ")
}
authorName.text = stringBuilder
}
}
If you fancy read Realm Recycler Adapter in detail.
So, the list is displayed as:
Until tomorrow…

