0

的文本我創建這些方法來定義數據綁定創建於Android和短整型值不同InverseBindingAdapter:EditText上

@BindingAdapter("android:text") 
public static void setShortText(TextView view, short value) { 
    view.setText(String.valueOf(value)); 
} 

@InverseBindingAdapter(attribute = "android:text") 
public static Short getShortText(TextView view) { 
    try { 
     return Short.parseShort(view.getText().toString()); 
    } catch (NumberFormatException e) { 
     return null; 
    } 
} 

@BindingAdapter("android:text") 
public static void setIntText(TextView view, int value) { 
    view.setText(String.valueOf(value)); 
} 

@InverseBindingAdapter(attribute = "android:text") 
public static Integer getIntText(TextView view) { 
    try { 
     return Integer.parseInt(view.getText().toString()); 
    } catch (NumberFormatException e) { 
     return null; 
    } 
} 

並結合我的短期和整型變量的觀點。當我建立我的項目時,自動生成的java使用前三種方法,而且它沒有使用第四種方法(getIntText),問題在這裏。它使用整數字段getShortText並導致編譯時間casting錯誤。

我很驚訝,數據綁定正確地檢測到它應該使用setIntText設置整數值的意見,但它無法識別inverseBinding。

我該如何解決這個問題?

請注意,因爲我想有當空

android:text="@={`` + obj.field} 

回答

0

數據我找到一個解決方案結合控制我不想從下面的技術使用,但真的不能滿足自己。 我將getIntText返回值從Integer更改爲int並且問題得到解決。 如果有人找到其他更好的解決方案,請讓我知道。

@InverseBindingAdapter(attribute = "android:text") 
public static int getIntText(TextView view) { 
    try { 
     return Integer.parseInt(view.getText().toString()); 
    } catch (NumberFormatException e) { 
     return -1; 
    } 
}