Home > Enterprise >  Get current ArrayList length after removing an element in onBindViewHolder()
Get current ArrayList length after removing an element in onBindViewHolder()

Time:12-23

How do I get the current ArrayList length after removing an element from it?

I have a fragment that contains a RecyclerView. In the adapter I delete the respective element in the list in the onBindViewHolder() by clicking on an icon - everything works wonderfully.

But after removing an element, I need the array length in my fragment.

How do I get this, what method do I need to call from the onbindViewHolder()?

Adapter Code:

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

    val cR = context.contentResolver
    val type = cR.getType(listeImg[position])


    if(type.toString().contains("mp4"))
    {

        val con = MediaController(context)

        con.setAnchorView(holder.imageForList)
        con.setMediaPlayer(holder.playing)


        holder.playing.setOnPreparedListener {it->
            it.setVolume(0F,0F)
            it.isLooping =  true


        }
        holder.playing.keepScreenOn = true
        holder.playing.setVideoURI(listeImg[position])
        holder.playing.start()
        holder.playVideo.visibility = View.VISIBLE
        holder.playing.visibility = View.VISIBLE
    }
    else
    {
        if(listeImg.size == 0)
        {
            holder.delete.visibility = View.INVISIBLE
        }
        else if(listeImg.size > 0)
        {
            holder.delete.visibility = View.VISIBLE

        }
        holder.playing.visibility = View.INVISIBLE
        holder.playVideo.visibility = View.INVISIBLE

        Glide.with(context).load(listeImg[position]).placeholder(R.drawable.no_image)
            .skipMemoryCache(false)
            .into(holder.imageForList)
    }
    
    holder.delete.setOnClickListener {
        listeImg.removeAt(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position,listeImg.size)

    }
    
}

override fun getItemCount(): Int {
    return listeImg.size
}

CodePudding user response:

if you are trying to get the list size of your data which is added to recyclerView. you can create your list at the top of the fragment class, and initialize it with your list of where come from. then you can access this from all of your fragment.

also, you achieve it like this, create this method in your adopter.

fun getListSize(): List<YourListType> {
    return listeImg.size
}

then call it from your fragment. like ( yourAdopter.getListSize())

CodePudding user response:

I think you should first call

linkeimg.remove(getAdapterPosition());

than call

notifyItemRemoved(getAdapterPosition());

notifyItemRangeChanged(getAdapterPosition(),mDataSet.size());

CodePudding user response:

try like this using higher-order function.

class ExampleAdapter(
    val updatedListSize: (Int) -> Unit
): RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>(){
    
    override fun onBindViewHolder(holder: ViewHolder, position: Int){
        // after removing data from list
        updatedListSize.invoke(listeImg.size)
    }
}

and in fragment you can do something like below

val adapter = ExampleAdapter(){ updatedListSize ->
   // do updated operations
}
  • Related