Search code examples
androidkotlinandroid-databinding

Data set by data binding is not displayed in the view


I'm new to databinding.

I used the sample code to slowly follow along, but the view doesn't show any data.

There are no errors.

I also confirmed that the data was properly included by debugging.

But no data is set in xml.

Fragment.kt

class WritingRoutineFragment : Fragment() {
    var titleData: ArrayList<String>? = null
    var title: String? = null
    private lateinit var binding: FragmentWritingRoutineBinding

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

        arguments?.apply {
            titleData = getStringArrayList("title")
            title = titleData?.joinToString(" / ")
        }
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
//        val view: View = inflater.inflate(R.layout.fragment_writing_routine, container, false)
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_writing_routine, container,false)
        val view = binding.root

        return view
    }
    companion object {
        @JvmStatic
        fun newInstance(data: ArrayList<String>) =
            WritingRoutineFragment().apply {
                arguments = Bundle().apply {
                    putStringArrayList("title", data)
                }
            }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="data"
            type="com.example.writeweight.fragment.WritingRoutineFragment" />
    </data>
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".fragment.WritingRoutineFragment">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.AppBarOverlay"
            app:elevation="0dp">
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/body_part_detail_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@{data.title}"
                    android:textColor="@color/white"
                    android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                    android:layout_centerHorizontal="true" />
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Solution

  • You need binding.data = this somewhere after binding initialized. (ex: in onCreateView())

    this is WritingRoutineFragment.