I have created an app that works perfectly. I decided to make some changes to my app, that is, use Fragment instead of Activity at some places. This activity has a RecyclerView in it and that is used to load data which is fetched from the Firestore. Everything will remain the same except Fragment will be used instead of Activity.
Now the problem I have is with the recyclerView adapter where it takes context.
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
When a button is clicked, I have to check the context to take further action accordingly.
holder.binding.ibDelete.setOnClickListener {
when (context) {
is OrderActivity -> {
val activity = holder.itemView.context as? OrderActivity
activity?.viewSpecialRequest(
model.custom_request
)
}
is MyOrderDetailsActivity -> {
val activity = holder.itemView.context as? MyOrderDetailsActivity
activity?.viewSpecialRequest(
model.custom_request
)
}
is CargoFragment -> {
val activity = holder.itemView.context as? CargoFragment
activity?.editDeleteSpecialRequestDialog(
holder.binding.ibEditDeleteNote,
holder.binding.ibAddNote,
model.id,
model.custom_request
)
}
}
}
This is how I am calling from one of the activities.
val cargoListAdapter = CargoListAdapter(this@OrderActivity, mCargoList)
I tried to call from the fragment like below but I can see an error "Incompatible types: CargoFragment and Context" at is CargoFragment -> {
val cargoListAdapter = CargoListAdapter(requireContext(), mCargoList)
EDIT:
This is not part of the question but to show what I have done when there are a couple of buttons in the recyclerView and take action depending on which button is clicked. Someone has already asked about it in the accepted answer's comment box.
Now the onClickItem of the adapter class is changed as per my requirement. Where, as you can see, I have action:String and which will let me know which button is clicked and what function/action needs to be done further.
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>,
private val onClickItem: (pos: Int,addButton: ImageButton,deleteButton: ImageButton,model: Cargo,action:String) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
In the onBindViewHolder I have the following.
holder.binding.ibRemoveItem.setOnClickListener {
onClickItem(holder.adapterPosition,
holder.binding.ibAddNote,
holder.binding.ibDeleteNote,
model,
"removeItem")
}
Whenever a button is clicked a button onClickListener is set up as above and depending on the button I change the value for 'action'. As you can see, in the above example, it's 'removeItem' and hence from the below code the action that comes under 'removeItem' will take place.
Following is what I have in the 'CargoFragment'
when (action) {
"DeleteNote" -> {
///TAKE SOME ACTION HERE
}
"AddNote" -> {
///TAKE SOME ACTION HERE
}
"removeItem" -> {
///TAKE SOME ACTION HERE
}
This is what I have done to handle the situation where there are multiple buttons in the recyclerView and it's working without any flaws. I hope some experts will comment on this so that beginners can know whether this is OK to follow.