1

I have implemented DataBinding with MVVM pattern, here is my ViewModel class.

public class MainViewModel extends BaseObservable {
    private String data, data1;

    @Bindable
    public String getData1() {
        return data1;
    }

    public void setData1(String data1) {
        this.data1 = data1;
    }

    @Bindable
    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
        notifyPropertyChanged(BR.data);
    }
}

now the problem is i can see BR.data there but not able to get BR.data1, how to use notifyPropertyChanged() for data1 variable.

I have tried to clean the project, also tried with rebuild it but didn't help me.

Here is my build.gradle file

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dataBinding {
        enabled = true
    }

    defaultConfig {
        ...
        ...
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

}
Ravi
  • 34,851
  • 21
  • 122
  • 183

4 Answers4

8

Since AndroidX is here

So, all you do need to change your import from

import your_package_name.BR

with below code

import androidx.databinding.library.baseAdapters.BR

Also, check your lifecycle library new version

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
Suraj Gupta
  • 879
  • 7
  • 6
4

I have found few ways to solve this.

1) Try with Invalidate caches and restart in android studio.
2) As yigit have mentioned, it is android studio bug, you can directly use variable instead of waiting for variable name to come as suggestion.

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
4

I can confirm what @jee states works. However if using androidx, the import statement will change to:

import androidx.databinding.library.baseAdapters.BR

Invalidate is not 100% needed. I just cleaned and rebuilt for my references to work again.

I am sure this is a Studio bug as @yigit mentioned but there are no bug reports anywhere confirming this.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
2

I have tried following and it is working now:

1. Change import of BR to import com.android.databinding.library.baseAdapters.BR;

2.file->invalidate cahces/restart

jee
  • 524
  • 4
  • 7