J. Pichardo J. Pichardo - 3 months ago 18
Android Question

Android Binding with resources

I'm working on a Android app, with the purpose of experiment with data binding, and researching I found a line that was similar to this (the one I wrote):

@{@string/dateFormat(person.birthday.year, person.birthday.month + person.birthday.day)}


And the string resource is:

<string name="dateFormat">%2$d/%3$d/%1$d</string>


In the example says it should work but I keep getting this error:

No resource found that matches the given name (at \u0027text\u0027 with value \u0027@{@string/dateFormat(person.birthday.year,person.birthday.month+1,person.birthday.day)\u0027)


The xml of the binding is:

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{@string/dateFormat(user.birthday.year, user.birthday.month + 1, user.birthday.day)}"/>


And the person model is:

public class Person implements Serializable{

private String firstName;
private String lastName;
private String phoneNumber;
private PickedDate birthday;
// Irrelevant Code Omitted


Where the PickedDate is:

public class PickedDate {

private final ObservableInt year;
private final ObservableInt month;
private final ObservableInt day;


As I see it seems to be mistaking the quotes with unicode values.

I researched but couldn't find any other place with a similar binding.

The example is from georgemount/DatePicker

Thanks in advance

EDIT:/



The problem occurred some times, it fixed by rebuilding the project, it seems that it wasn't loading some libraries.


Answer

I have copied Your layout and model and try to put it into hello world project. It can be compiled successfully without the

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.activity_main,null,false);
        Person lUser = new Person();
        Person.PickedDate lBirthday = new Person.PickedDate();
        lBirthday.getDay().set(3);
        lBirthday.getMonth().set(4);
        lBirthday.getYear().set(5);
        lUser.setBirthday(lBirthday);
        binding.setUser(lUser);
        setContentView(binding.getRoot());
    }
}

Model Class

public class Person implements Serializable {
    private String firstName;
    private String lastName;
    private String phoneNumber;
    private PickedDate birthday;
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String pFirstName) {
        firstName = pFirstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String pLastName) {
        lastName = pLastName;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String pPhoneNumber) {
        phoneNumber = pPhoneNumber;
    }
    public PickedDate getBirthday() {
        return birthday;
    }
    public void setBirthday(PickedDate pBirthday) {
        birthday = pBirthday;
    }
    public static class PickedDate {
        private final ObservableInt year;
        private final ObservableInt month;
        private final ObservableInt day;
        public PickedDate() {
            year = new ObservableInt();
            month = new ObservableInt();
            day = new ObservableInt();
        }
        public ObservableInt getYear() {
            return year;
        }
        public ObservableInt getMonth() {
            return month;
        }
        public ObservableInt getDay() {
            return day;
        }
    }
}

activity_main.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="user"
            type=".Person"/>
    </data>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{@string/dateFormat(user.birthday.year, user.birthday.month + 1, user.birthday.day)}"/>
    </LinearLayout>
</layout>

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "example.com.testerapplication"
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }
    dataBinding {
        enabled = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}
Comments