无法获取简单的TextView以与Android数据绑定一起使用 [英] Cannot Get a simple TextView to work with Android Data Binding

查看:74
本文介绍了无法获取简单的TextView以与Android数据绑定一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我终于编译了我的Android数据绑定代码。现在,它不符合我的预期。当我从 Activity 活动中调用 setCurrentShopperInfo()时,我的 TextView (s)不会使用我设置的 String s的值进行更新。

OK, I finally have my Android Data Binding code compiling. Now, It does not work as I expect. When I call setCurrentShopperInfo() from my Activity, My TextView(s) do not update with the Strings values I'm setting.

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

NOTE: I also tried this with using ObservableField(s), same result.

这里有 HAS 是我不理解的地方:

There HAS to be something I'm not understanding here:

布局:

<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] ) );
    }

GRADLE:

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:

不知道在这里有什么意义,但是我从<$ c $中调用 setCurrentShopperInfo()后看到了c>活动:

Not sure what what would be significant here, but I see this after I call setCurrentShopperInfo() from my Activity:

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

NOTIFY属性已更改调试

NOTIFY PROPERTY CHANGED Debugging

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

I stepped into the notifyPropertyChanged() method and mCallbacks size is 0 --- is that correct?

/**
     * 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设置数据绑定的方法,其中最终将其称为:

I also tried the ObservableField way of setting data binding where ultimately this is called:

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

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

and mCallbacks is again null and I'm thinking that is an issue but don't know how to debug it.

推荐答案

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

My problem was that in OnCreate() I was calling setContentView() twice ...once for the data binding and then again the old way (which apparently overwrote the data binding):

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

//other code here

setContentView(R.layout.activity);

这篇关于无法获取简单的TextView以与Android数据绑定一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆