0

i try to build a sample app using the navigation drawer activitiy and a listview in one of the sub fragments. Therefor i created the project and then added a list fragment to res.layout.navigation.mobile_navigation.

So far so nice. When compiling in ItemRecyclerViewAdapter the following error occurs:

import com.example.mydatabindingtest.databinding.FragmentItemBindingImpl;
                                                ^
  symbol:   class FragmentItemBindingImpl
  location: package com.example.mydatabindingtest.databinding

After reading a lot i suppose this file should be generates automatically when creating the app. So the question is why is the file not created. Or if i'm wrong how should the file look like.

I'm using:

  • Android Studio 4.2
  • Gradle 4.2.0
  • Kotlin 1.5.0

All kept up to date by updating when prompted.

Here the code snippets i think you need to help:

build.gradle(:app)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.mydatabindingtest"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        dataBinding true
        viewBinding true
    }
}

fragment_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data class="FragmentItemBinding">
        <variable
            name="item"
            type="com.example.mydatabindingtest.ui.listview.placeholder.PlaceholderContent.PlaceholderItem" />
    </data>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/item_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem"
            android:text="@{item.id}"/>

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem"
            android:text="@{item.content}"/>
    </LinearLayout>
</layout>

MyItemRecyclerViewAdapter.kt

package com.example.mydatabindingtest.ui.listview

import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.mydatabindingtest.R

import com.example.mydatabindingtest.ui.listview.placeholder.PlaceholderContent.PlaceholderItem
import com.example.mydatabindingtest.ui.listview.databinding.FragmentItemBinding

/**
 * [RecyclerView.Adapter] that can display a [PlaceholderItem].
 * TODO: Replace the implementation with code for your data type.
 */
class MyItemRecyclerViewAdapter(
    private val values: List<PlaceholderItem>
) : RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder>() {

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

        return ViewHolder(
            FragmentItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = values[position]
        holder.idView.text = item.id
        holder.contentView.text = item.content
    }

    override fun getItemCount(): Int = values.size

    inner class ViewHolder(binding: FragmentItemBinding) : RecyclerView.ViewHolder(binding.root) {
        val idView: TextView = binding.itemNumber
        val contentView: TextView = binding.content

        override fun toString(): String {
            return super.toString() + " '" + contentView.text + "'"
        }
    }
}

I appologize if the question is already answered. To defend my self, i did not find the answer after searching for several hours, since the answers i read are farly old and did not solve my problem.

So your help will be apreciated.

Best regards

EDIT:

After domicoders question, i rebuilt the hole project. Now the three following errors are shown:

e: G:\Projekte\Android\MyDataBindingTest\app\src\main\java\com\example\mydatabindingtest\ui\listview\MyItemRecyclerViewAdapter.kt: (11, 50): Unresolved reference: databinding
e: G:\Projekte\Android\MyDataBindingTest\app\src\main\java\com\example\mydatabindingtest\ui\listview\MyItemRecyclerViewAdapter.kt: (24, 13): Unresolved reference: FragmentItemBinding
e: G:\Projekte\Android\MyDataBindingTest\app\src\main\java\com\example\mydatabindingtest\ui\listview\MyItemRecyclerViewAdapter.kt: (41, 37): Unresolved reference: FragmentItemBinding

The reason for these messages is that the required class is not existing. But why does android studio insert the questionable import when the file is not created automatically. Or am i mistanken at some point?

Best regards

Gregor Fey
  • 41
  • 4
  • You say the error is for `import com.example.mydatabindingtest.databinding.FragmentItemBindingImpl` but the code you posted shows `import com.example.mydatabindingtest.ui.listview.databinding.FragmentItemBinding`. Which is it? – dominicoder May 14 '21 at 03:28
  • Ok, i did not notice the difference in the package name. I'll try to reproduce the output and get clear about the package. @dominicoder – Gregor Fey May 14 '21 at 11:47

1 Answers1

0

As stated here, enable databinding in your App Level build.gradle file

android {
    ...
    dataBinding{
        enabled=true
    }
}
mariko
  • 119
  • 2
  • 7