2016-08-22 40 views
5

this commentAndroid Data Binding tutorial我應該有一個context變量時可用數據綁定:錯誤綁定提供上下文變量與BaseObservable

A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

我使用擴展BaseObservable設置一類我數據,但無論我如何努力把在context變量在我@Bindable方法我得到的編譯時錯誤:

java.lang.RuntimeException: failure, see logs for details. @Bindable associated with method must follow JavaBeans convention getStyledName(android.content.Context)

至於我可以告訴大家,我遵循JavaBeans約定。我已經將錯誤記錄到了this line in BrUtil,但那也沒有給我太多幫助。

我試圖改變我的方法和參數的名字,我已經嘗試添加在<variable>contextandroid.content.Context一個<import>,但沒有說什麼不同。我的User.java下面有兩個@Bindable方法,儘管getName()沒有問題,但試圖使用context變量的方法並沒有,所以我認爲這與我的其他數據綁定設置無關。

相關的代碼:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools"> 

    <data> 
     <variable name="user" type="com.kasra.androidsandbox.User" /> 
    </data> 

    <LinearLayout 
     android:id="@+id/main_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/main_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

     <TextView 
      android:id="@+id/main_textview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="@{user.getStyledName(context)}" /> 
    </LinearLayout> 
</layout> 

User.java

public class User extends BaseObservable { 
    private String name; 

    public User(String name) { 
     this.name = name; 
    } 

    @Bindable 
    public String getName() { 
     return this.name; 
    } 

    @Bindable 
    public CharSequence getStyledName(Context ctx) { 
     int color = ctx.getResources().getColor(R.color.branded_pink); 

     Spannable textSpan = new SpannableString(name); 
     textSpan.setSpan(new ForegroundColorSpan(color), 0, textSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

     return textSpan; 
    } 

} 

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 
    User user = new User("Kasra"); 
    binding.setUser(user); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar); 
    setSupportActionBar(toolbar); 
} 
+1

我相信這可能是與數據綁定庫本身的錯誤,所以我也crossposted這到Android錯誤跟蹤:https://code.google.com/p/android/issues/detail?id=220621 –

+0

你應該能夠通過刪除@ @ Bindable'註釋 –

+0

@ChadBingham來解決它,而在這種情況下,我不需要'@B在我的實際應用中,我認爲,我會從中受益非淺。 –

回答

13

我不知道這是一個錯誤還是有意的行爲,但我自己也觀察過這種行爲,所以你並不瘋狂。

解決方法是在您的用戶中創建一個未標記爲@Bindable的方法 - 需要上下文的方法。此方法不受JavaBeans命名約定的約束。

重要:爲了確保它被適當地更新,它應該將任何可能改變的@Bindable字段作爲輸入。

User.java

public class User extends BaseObservable { 
    private String name; 

    public User(String name) { 
     this.name = name; 
    } 

    @Bindable 
    public String getName() { 
     return this.name; 
    } 

    public CharSequence getStyledName(Context ctx, String name) { 
     // construct styled name here 
    } 
} 

,並在佈局:

<TextView 
     android:id="@+id/main_textview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="@{user.getStyledName(context, user.name)}" /> 
+0

很酷的workaroud。 – muthuraj

0

由於Android 2.3,你可以宣佈你的綁定依賴於@Bindable標註內場。

@Target({ElementType.FIELD, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface Bindable { 
    String[] value() default {}; 
} 

在你的情況,你會寫你的函數類似這樣的

https://medium.com/google-developers/android-data-binding-dependent-properties-516d3235cd7c

+1

你真的嘗試過嗎?它仍在抱怨JavaBeans慣例。 –

+0

@RobertoB。是的,我現在一直在使用它。 – oldergod