android – when using databinding, recyclerview don't show anything-ThrowExceptions

Exception or error:

I’m using databinding now. when I inflate recyclerview using databinding, there was no items. the log in adapter also didn’t show anything. but when using no databinding, I could show results I want. I dont understand why using databinding didn’t show anything.
as below, the viewDataBinding is databinding variable. If I use this variable, the result don’t show. but If I use exclude that, the result show right.

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>
        <variable
            name="viewmodel"
            type="com.haii.schedulemanager.schedule.ScheduleViewModel" />
    </data>


<FrameLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/scheduleListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Main fragment"
        android:textSize="24sp"
        android:visibility="gone" />
</FrameLayout>
</layout>

Fragment

class ScheduleFragment : DaggerFragment() {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory

    private val viewModel by viewModels<ScheduleViewModel> { viewModelFactory }

    private lateinit var viewDataBinding: FragmentScheduleBinding

    private var itemList = listOf<ScheduleItem>()
    private lateinit var scheduleAdapter: ScheduleAdapter
    private lateinit var manager : RecyclerView.LayoutManager

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_schedule, container, false)
        viewDataBinding = FragmentScheduleBinding.inflate(inflater, container, false).apply {
            viewmodel = viewModel
        }
        return view
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        viewDataBinding.lifecycleOwner = this.viewLifecycleOwner
        manager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        scheduleAdapter = ScheduleAdapter()
        viewDataBinding.scheduleListView.layoutManager = manager
        viewDataBinding.scheduleListView.adapter = scheduleAdapter

        init()
        viewModel.getWeek()
    }

    private fun init(){
        viewModel.item.observe(this, Observer{
            itemList = it
            scheduleAdapter.removeItems()
            scheduleAdapter.addItems(itemList)
            viewDataBinding.scheduleListView.adapter?.notifyDataSetChanged()

        })

    }

}

Adapter

class ScheduleAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    lateinit var view : View
    var itemList =  arrayListOf<ScheduleItem>()


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        if(viewType == HEADER_TYPE){
            view = inflater.inflate(R.layout.schedule_header,parent,false)
            return HeaderViewHolder(view)
        }
        else if(viewType == ITEM_TYPE){
            view = inflater.inflate(R.layout.schedule_item,parent,false)
            return ItemViewHolder(view)
        }else{

            return HeaderViewHolder(view)
        }
    }

    override fun getItemCount(): Int = itemList.size

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

        if(holder is HeaderViewHolder){
            holder.startDay.text = itemList.get(position).startDay
        }
        else if(holder is ItemViewHolder){
            holder.time.text = itemList.get(position).startTime+"~"+itemList.get(position).endTime
            holder.title.text = itemList.get(position).title
            holder.participant.text = itemList.get(position).participant
            holder.title.text = itemList.get(position).title
        }
    }


    class HeaderViewHolder internal constructor(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var startDay: TextView

        init {
            startDay = itemView.findViewById(R.id.startDay)
        }
    }

    class ItemViewHolder internal constructor(itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var time: TextView
        var title: TextView
        var participant: TextView
        var place: TextView


        init {
            time = itemView.findViewById(R.id.time)
            title = itemView.findViewById(R.id.title)
            participant = itemView.findViewById(R.id.participant)
            place = itemView.findViewById(R.id.place)

        }

    }

    override fun getItemViewType(position: Int): Int {
        return itemList.get(position).viewType
    }

    override fun getItemId(position: Int): Long {
        return itemList.get(position).id.toLong()
    }

    fun addItems(item : List<ScheduleItem>){
        itemList.addAll(item)
    }

    fun removeItems(){
        itemList.clear()
    }


}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *