RecyclerView ListAdapter Doesn't Show A Particular Item

Viewed 414

My RecyclerView ListAdapter doesn't show a particular item :

enter image description here

As can be seen in the picture, all the items of list are displayed except the item at Index 8. There is nothing wrong with this Item, as I checked through Logs. Moreover, if I provide a new list with different contents , some items are shown and some aren't.

Here is my Code :

Fragment :

class UnicornFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding : FragmentUnicornBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_unicorn, container, false)
        binding.lifecycleOwner = this

        val unicornList = UnicornFragmentArgs.fromBundle(arguments!!).unicornsList

        val adapter = UnicornAdapter()
        binding.unicornsView.adapter = adapter
        Log.e("Unicorn Fragment", "${unicornList.toMutableList().size}")
        adapter.submitList(unicornList.toMutableList())

        return binding.root
    }
}

Adapter :

class UnicornAdapter : androidx.recyclerview.widget.ListAdapter<Unicorn, UnicornAdapter.UnicornViewHolder> (DiffCallback){

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): UnicornViewHolder {
        return UnicornViewHolder(ItemUnicornBinding.inflate(LayoutInflater.from(parent.context)))
    }

    class UnicornViewHolder(private var binding : ItemUnicornBinding) : RecyclerView.ViewHolder(binding.root){
        fun bind(unicorn: Unicorn) {
            binding.unicorn = unicorn

            binding.executePendingBindings()

            Log.e("UnicornAdapter", "Question : ${unicorn.questionText}, Answer : ${unicorn.answerText}")
        }

    }

    override fun onBindViewHolder(holder: UnicornViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    companion object DiffCallback : DiffUtil.ItemCallback<Unicorn>() {
        override fun areItemsTheSame(oldItem: Unicorn, newItem: Unicorn): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: Unicorn, newItem: Unicorn): Boolean {
             return  oldItem.questionText == newItem.questionText
        }

    }
}

fragment_unicorn.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/unicorns_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

item_unicorn.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="unicorn"
            type="com.example.unicorn.network.Unicorn" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/question_text_view"
            style="@style/MainHeading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:fontFamily="sans-serif-black"
            android:gravity="start"
            android:text="@{unicorn.questionText}"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="This is a sample Question" />

        <TextView
            android:id="@+id/answer_text_view"
            style="@style/SubHeading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="start"
            android:textSize="20sp"
            android:text="@{unicorn.answerText}"
            app:layout_constraintStart_toStartOf="@+id/question_text_view"
            app:layout_constraintTop_toBottomOf="@+id/question_text_view"
            tools:text="Sample Answer" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Logs :

2020-07-01 19:17:51.093 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 1. His brother was accused ……theft., Answer : c)   Of 
2020-07-01 19:17:51.141 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 2. Avail yourself …….this opportunity., Answer : a)    Of
2020-07-01 19:17:51.187 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 3. He is bent ……mischief., Answer : b) On    
2020-07-01 19:17:51.228 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 4. I cannot comply ……..your request, Answer : d)   With
2020-07-01 19:17:51.275 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 5. He is devoid ………all sense of decency. , Answer : b) Of     
2020-07-01 19:17:51.324 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 6. He is endowed …….a strong will., Answer : a)    With  
2020-07-01 19:17:51.369 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 7. Portia was the heiress …….her father’s property, Answer : c)    To
2020-07-01 19:17:51.412 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 8. I congratulate you …….your brilliant success. , Answer : b) On  
2020-07-01 19:17:54.585 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 9. He is blind …….his drawbacks. , Answer : a) To
2020-07-01 19:17:54.679 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 10.    He did not agree ……..my proposal. , Answer : b) To    
2020-07-01 19:17:58.187 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 11.    He as well as you ……guilty., Answer : b)    Is    
2020-07-01 19:17:58.260 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 12.    They are working to live. Identify the verb in this sentence., Answer : b)  Are working 
2020-07-01 19:17:58.328 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 14.    An example for infinite verb is:, Answer : b

As can be seen , the item at Index 8 has values , just like all other items.

Any help would be highly appreciated !

2 Answers

The problem was here :

override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): UnicornViewHolder {
        return UnicornViewHolder(ItemUnicornBinding.inflate(LayoutInflater.from(parent.context)))
    }

Changed it to this instead, and it worked :

 override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): UnicornViewHolder {
            return UnicornViewHolder(ItemUnicornBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        }

Try to Override the getItemViewType in your Adapter View to return position

@Override
public int getItemViewType(int position) { return position; }
Related