Search code examples
androidarraylistkotlinandroid-viewpager

add in arraylist automaticlly and error while try to clear() arraylist


In my module ,I pass arraylist threw intent. but when i open my activity it added automatic. When i try to clear my arraylist in onResume() of parent activity it seems this type of error :-

The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!

My code is here :-

This is PreExamActivity.kt here click event of exam start Button.

startExam.setOnClickListener {
        //getMarks()
        calculation()
        Common.examqueList = null
        if (numOfQueInSelectedChapter > totalQuestion) {
            val intent = Intent(this, ExamActivity::class.java)
            Common.examqueList = examQueList
            startActivity(intent)
        } else {
            Toast.makeText(this, "Check yr question", Toast.LENGTH_SHORT).show()
        }
    }

override fun onResume() {
    super.onResume()
    Common.examqueList?.clear()
}

Here is my ExamActivity.kt** :-

    queArray = Common.examqueList
    val adpter: PagerAdapter = ExamQuestionPageView(this, queArray!!, question_img_path)
    qPager.adapter = adpter

Solution

  • Call notifyDataSetChanged after clearing the ArrayList.

    Whenever you add/remove the item from the list or modifying the size of the list, you have to call notifyDataSetChanged() to see the changes in the recycler view.

    override fun onResume() {
        super.onResume()
        Common.examqueList?.clear()
        adapter.notifyDataSetChanged();
    }