Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adapter does not render elements in recyclerview, without errors in logs

Adapter does not render elements in recyclerview, and there are no errors in logcat.
I changed the background of the recyclerview and I know that it is displayed, but the elements that I add to it do not appear.Adapters written in the same way work on a different activity.

mainActivity

class MainActivity : AppCompatActivity(), DomainAdapter.OnClickListener {
    lateinit var adapter: DomainAdapter
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.rvDomains.layoutManager = LinearLayoutManager(this)

        binding.btnSearch.setOnClickListener{
            adapter = DomainAdapter(this, mutableListOf(DomainCVE(binding.etSearch.text.toString())), true)
            adapter.notifyDataSetChanged()
        }
    }

    override fun onClick(domain: DomainCVE) {
        val intent = Intent(this, DomainActivity::class.java)
        intent.putExtra("domain", domain)
        startActivity(intent)
    }
}

adapter

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class DomainAdapter(private val context: Context, private val domain: MutableList<DomainCVE>, private val load: Boolean): RecyclerView.Adapter<DomainAdapter.DomainViewHolder>() {

    class DomainViewHolder(item: View): RecyclerView.ViewHolder(item){
        val binding = DomainItemsBinding.bind(item)

        fun bind(domain: DomainCVE, listener: OnClickListener) = with(binding){
            itemView.setOnClickListener {
                listener.onClick(domain)
            }
        }
    }

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

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

    override fun onBindViewHolder(holder: DomainViewHolder, position: Int) {
        holder.binding.tvDomainName.text = domain[position].address
        if (load){
            holder.binding.ivDone.visibility = View.INVISIBLE
            holder.binding.ivLoad.visibility = View.VISIBLE
        }else{
            holder.binding.ivDone.visibility = View.VISIBLE
            holder.binding.ivLoad.visibility = View.INVISIBLE
        }
    }

    interface OnClickListener {
        fun onClick(domain: DomainCVE)
    }
}

>Solution :

so 2 reasons possible:

  1. you’re creating your adapter in onClick of a button, which would mean your data is displayed after some button is clicked, consider doing something like:
adapter = DomainAdapter(this, mutableListOf(DomainCVE(binding.etSearch.text.toString())), true)
binding.rvDomains.adapter = adapter
binding.btnSearch.setOnClickListener{
    adapter.updateData(mutableListOf(DomainCVE(binding.etSearch.text.toString())))
}
  1. you’re not assigning the adapter to the recycler view in the onClick
binding.btnSearch.setOnClickListener{
    adapter = DomainAdapter(this, mutableListOf(DomainCVE(binding.etSearch.text.toString())), true)
    binding.rvDomains.adapter = adapter
    adapter.notifyDataSetChanged()
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading