skip to Main Content

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
}

4

Answers


  1. 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
        notifydatachanged()
    }
    

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

    Login or Signup to reply.
  2. I think you should first call

    linkeimg.remove(getAdapterPosition());

    than call

    notifyItemRemoved(getAdapterPosition());

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

    Login or Signup to reply.
  3. 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
    }
    
    Login or Signup to reply.
  4. First you remove a item and notified the array list then down code is used

    adapter = SearchMerchantsAdapter(listData,::checkCurrentListSize) // This code is Fragment Adapter Code
    
    class SearchMerchantsAdapter(var dataList: ArrayList<GlobalSearchItemData>,private val getCurrentListSize: (Int) -> Unit) : RecyclerView.Adapter<MerchantsAdapter.ViewHolder>() {
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            getCurrentListSize(dataList.size)
        }
    }
    
    fun checkCurrentListSize(i: Int) { // This Fun Use High Order Funtion through data get (checkCurrentListSize) fun name use in Adapter Call Line
        Log.d("checkArraySizeGet", i.toString())
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search