无法获得简单的TextView来使用Android数据绑定

时间:2016-04-13 01:15:36

标签: android data-binding

好的,我终于有了我的Android数据绑定代码编译。现在,它没有像我期望的那样工作。当我从setCurrentShopperInfo()致电Activity时,我的TextView(s)不会使用我设置的String值进行更新。

注意:我也尝试使用ObservableField(s),结果相同。

HAS 是我在这里不理解的东西:

布局:

<data>
        <variable name="currentShopperViewModel" type="ts.kiosk.app.checkout.viewmodels.CurrentShopperViewModel" />
        <import type="android.view.View" />
    </data>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

             <RelativeLayout
                android:id="@+id/checkout_titlebar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_header_height"
                android:layout_alignParentTop="true"
                android:background="@color/header_bkgrd">
                <ImageView
                    android:id="@+id/img_ts_logo"
                    android:layout_width="@dimen/app_icon_width"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitCenter"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/app_icon_margin"
                    android:layout_marginRight="@dimen/app_icon_margin"
                    android:src="@drawable/touchsides_logo_notext"/>
                 <TextView
                     android:id="@+id/text_appname"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/title_checkout"
                     android:layout_toRightOf="@+id/img_ts_logo"
                     android:layout_centerVertical="true"
                     style="@style/screenTitle"/>

                 <LinearLayout
                     android:visibility="visible"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:orientation="horizontal"
                     android:layout_toRightOf="@+id/text_appname"
                     android:layout_toLeftOf="@+id/img_settings_menu"
                     android:paddingRight="20dip"
                     android:paddingLeft="20dip"
                     android:gravity="center_horizontal">
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoName}"
                         style="@style/previous_transaction_titlebar"></TextView>
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoCashback}"
                         style="@style/previous_transaction_titlebar_cashback"
                         android:layout_marginLeft="20dip"></TextView>
                     <TextView
                         android:layout_width="wrap_content"
                         android:layout_height="match_parent"
                         android:text="@{currentShopperViewModel.textShopperInfoPoints}"
                         style="@style/previous_transaction_titlebar_points"
                         android:layout_marginLeft="20dip"></TextView>
                 </LinearLayout>

活性

mCurentShopperViewModel = new CurrentShopperViewModel(this);
        final ActivityCheckoutNewBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_checkout_new);
        binding.setCurrentShopperViewModel(mCurentShopperViewModel);

查看模特:

public class CurrentShopperViewModel extends BaseObservable{

    Context mCtx;

    //public ObservableBoolean showShopperInfo = new ObservableBoolean();


    private String textShopperInfoName;
    private String textShopperInfoCashback;
    private String textShopperInfoPoints;


    @Bindable
    public String getTextShopperInfoName() {
        return this.textShopperInfoName;
    }
    @Bindable
    public String getTextShopperInfoCashback() {
        return this.textShopperInfoCashback;
    }
    @Bindable
    public String getTextShopperInfoPoints() {
        return this.textShopperInfoPoints;
    }


    public void setTextShopperInfoName(String name) {
        this.textShopperInfoName = name;
        notifyPropertyChanged(BR.textShopperInfoName);
    }
    public void setTextShopperInfoCashback(String cashback) {
        this.textShopperInfoCashback = cashback;
        notifyPropertyChanged(BR.textShopperInfoCashback);
    }
    public void setTextShopperInfoPoints(String points) {
        this.textShopperInfoPoints = points;
        notifyPropertyChanged(BR.textShopperInfoPoints);
    }


    public CurrentShopperViewModel(Context context) {
        mCtx = context;
    }


    public void  setCurrentShopperInfo(CardStructure cardStructure ) {

        //show(true);

        int[] card = CardUtil.getInstance().getBalance(cardStructure);

        setTextShopperInfoName( String.format( "%s %s", mCtx.getResources().getString(R.string.item_wallet_desposit) , Formatter.formatCurrency(CheckoutApplication.LOCALE,card[0]) ) );
        setTextShopperInfoCashback(  String.format( "%s %s", mCtx.getResources().getString(R.string.item_wallet_desposit) , Formatter.formatCurrency(CheckoutApplication.LOCALE,card[0]) )  );
        setTextShopperInfoPoints ( String.format( "%s %s",  mCtx.getResources().getString(R.string.item_loyalty_points),card[1] ) );
    }

摇篮:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    }
}



apply plugin: 'com.android.application'
apply plugin: 'crashlytics'


repositories {
    maven { url 'http://download.crashlytics.com/maven' }
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}

android {
    compileSdkVersion 20
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "ts.kiosk.app.checkout"
        minSdkVersion 16
        targetSdkVersion 16
        versionCode 2
        versionName "0.0.2.258"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }


    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
        }
    }


    dataBinding{
        enabled = true
    }
}



dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'net.danlew:android.joda:2.7.2'
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    //compile 'com.squareup.okhttp:okhttp:2.0.0'

    // Robolectric
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.apache.maven:maven-ant-tasks:2.1.3' // fixes issue on linux/mac
    testCompile 'org.robolectric:robolectric:3.0'

    compile project(':asterixmodule')
    compile project(':servicemodule')
    compile project(':sdfclient')


}

android {

    packagingOptions {
        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.properties'
        exclude 'META-INF/maven/com.squareup.okhttp/okhttp/pom.xml'
    }
}

logcat的:

不确定这里有什么重要意义,但是在我从setCurrentShopperInfo()致电Activity后我看到了这一点:

04-12 21:31:30.252 866-866/ts.kiosk.app.checkout E/java.lang.StackTraceElement: InterfaceError

NOTIFY PROPERTY CHANGED调试

我进入了notifyPropertyChanged()方法并且mCallbacks大小为0 ---这是正确的吗?

/**
     * Notifies listeners that a specific property has changed. The getter for the property
     * that changes should be marked with {@link Bindable} to generate a field in
     * <code>BR</code> to be used as <code>fieldId</code>.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    public void notifyPropertyChanged(int fieldId) {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, fieldId, null);
        }
    }

我还尝试了ObservableField设置数据绑定的方法,最终调用它:

public synchronized void notifyChange() {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, 0, null);
        }
    }

和mCallbacks再次为空,我认为这是一个问题但不知道如何调试它。

1 个答案:

答案 0 :(得分:2)

我的问题是在OnCreate()中我调用了setContentView()两次...一次用于数据绑定,然后再用旧方法(显然覆盖了数据绑定):

DataBindingUtil.setContentView(this, R.layout.activity)

//other code here

setContentView(R.layout.activity);