Foxtrot 1-5
Foxtrot 1-5

Reputation: 379

Android Kotlin RecyclerView Search Item with EditText with data from Data Class

So I have a RecyclerView which data is populated with data from some Data Class. I want to Apply a search item function for this recyclerview. Back when I don't use Data Classes, I followed this tutorial (The tutorial is in Java).

Now that I'm using a data class since I fetch the data that populate the recyclerview from an API endpoint, I'm quite confused on how to apply the search function in the current recyclerview. (I fetch the data using library Retrofit if you wondering.)

This is the code snippet from the fragment:

RefundListFragment.kt

private fun fetchRefundListData() {
    NetworkConfig().getRefundListDetailService().getRefundList().enqueue(object :
        Callback<RefundListPOJODataClass> {
        override fun onFailure(call: Call<RefundListPOJODataClass>, t: Throwable) {
            ...
            ...
            ...

        }

        override fun onResponse(
            call: Call<RefundListPOJODataClass>,
            response: Response<RefundListPOJODataClass>
        ) {
            binding.refundProgressBar.visibility = View.GONE

            binding.rvRefundList.adapter =
                response.body()?.let { RefundListAdapter(it, this@RefundListFragment) }

            fun filterString(text: String) {
                val filteredList: List<RefundListPOJODataClass> = ArrayList()
                for (item in response.body()?.data!!) {
                    if (item != null) {
                        if (item.buyerName?.toLowerCase()?.contains(text.toLowerCase())!!) {
                            filteredList.add(item)
                        }
                    }
                }
                adapter.filterList(filteredList)
            }


            binding.refundListSearchBar.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    filterString(s.toString())
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

            })

        }

    })
}

However it returned an error:

Unresolved reference: add

This is the recyclerview Adapter:

RefundListAdapter.kt

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

    private val refundedOrderId: TextView = itemView.refundedOrderId
    private val orderDateTime: TextView = itemView.orderDateTime
    private val refundedCustomerName: TextView = itemView.refundedCustomerName

    fun bind(
        refundHistory: RefundListPOJODataClassDataItem,
        clickListener: RefundListOnItemClickListener
    ) {
        refundedOrderId.text = refundHistory.orderId
        orderDateTime.text = refundHistory.orderDate
        refundedCustomerName.text = refundHistory.buyerName

        itemView.setOnClickListener {
            clickListener.onItemClicked(refundHistory)
        }
    }
}



class RefundListAdapter(
    private var refundList: RefundListPOJODataClass,
    private val itemClickListener: RefundListOnItemClickListener
) : RecyclerView.Adapter<RefundListItemHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RefundListItemHolder {
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.refund_list_item_layout, parent, false)
        return RefundListItemHolder(v)
    }

    override fun getItemCount(): Int {
        if (refundList.data != null) {
            return refundList.data!!.size
        } else {
            return 0
        }
    }

    override fun onBindViewHolder(holder: RefundListItemHolder, position: Int) {
        val refundHistory = refundList.data?.get(position)
        if (refundHistory != null) {
            holder.bind(refundHistory, itemClickListener)
        }
    }

    fun filterList(filteredList: List<RefundListPOJODataClass>) { //This is the function I called in the fragment
        refundList = filteredList.get(0)
        notifyDataSetChanged()
    }
}

interface RefundListOnItemClickListener {
    fun onItemClicked(refundHistory: RefundListPOJODataClassDataItem)
}

And this is how the data class looks like

RefundListPOJODataClass.kt

data class RefundListPOJODataClass(

    @field:SerializedName("data")
    val data: List<RefundListPOJODataClassDataItem?>? = null,

    @field:SerializedName("error")
    val error: Error? = null
)

data class RefundListPOJODataClassError(

    @field:SerializedName("msg")
    val msg: String? = null,

    @field:SerializedName("code")
    val code: Int? = null,

    @field:SerializedName("status")
    val status: Boolean? = null
)

data class RefundListPOJODataClassDataItem(

    @field:SerializedName("order_date")
    val orderDate: String? = null,

    @field:SerializedName("buyer_name")
    val buyerName: String? = null,

    @field:SerializedName("phone_number")
    val phoneNumber: String? = null,

    @field:SerializedName("status_refund")
    val statusRefund: String? = null,

    @field:SerializedName("order_id")
    val orderId: String? = null,

    @field:SerializedName("refund")
    val refund: Int? = null,

    @field:SerializedName("refund_date")
    val refundDate: String? = null
)

I want to search the recyclerview item by the attribute buyerName. How can I achieve it ? What should i change in my code? If there's any detail I missed to tell, Just let me know.

Upvotes: 0

Views: 381

Answers (1)

Ravi Kumar
Ravi Kumar

Reputation: 4528

Your filtered list is of type List, which is immutable. Change it to array list or MutableList:

val filteredList: ArrayList<RefundListPOJODataClass> = ArrayList()

Upvotes: 1

Related Questions