Ituoke Ajanlekoko Ituoke Ajanlekoko - 3 months ago 28
Android Question

Using Long Value In Android Databinding

I am trying to use

Long
value in
Android Databinding
but, just can't get it to work. This is everything I've tried

1: This worked fine. Until I started testing on pre-lollipop and I started experiencing crashes. I had to remove this
android:addTextChangedListener="@{outlet.onCapacityChange}"
from the XML

Java
@SerializedName("capacity")
@Expose
Long capacity
/**
*
* @return
* The capacity
*/
@Bindable
public Long getCapacity() {
return capacity;
}

/**
*
* @param capacity
* The capacity
*/
public void setCapacity(Long capacity) {
setAtomicCapacity(capacity);
Log.e(TAG+"Capacity", "" + capacity);
notifyPropertyChanged(BR.capacity);
}

public void setAtomicCapacity(Long basic) {
this.capacity = basic;
}

public TextWatcher onCapacityChange = new SimpleTextWatcher() {
@Override
public void onTextChanged(String newValue) {
Log.e(TAG+"Capacity", newValue);
setAtomicCapacity(Long.valueOf(newValue));
}
};

XML
<EditText
android:id="@+id/outletStockCapacity"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text='@{outlet.capacity != null ? String.valueOf(outlet.capacity) : ""}'
android:addTextChangedListener="@{outlet.onCapacityChange}"
android:singleLine="true"
android:imeOptions="actionNext"/>


2: After changing all
android:addTextChangedListener="@{outlet.onCapacityChange}"
to
android:text="@={String.valueOf(outlet.capacity)}"
using
=
Equal Operator
since two way binding is now provided by default.

Java
@SerializedName("capacity")
@Expose
Long capacity = 0L;
/**
*
* @return
* The capacity
*/
@Bindable
public Long getCapacity() {
return capacity;
}

/**
*
* @param capacity
* The capacity
*/
public void setCapacity(Long capacity) {
setAtomicCapacity(capacity);
Log.e(TAG+"Capacity", "" + capacity);
notifyPropertyChanged(BR.capacity);
}

public void setAtomicCapacity(Long basic) {
this.capacity = basic;
}

XML
<EditText
android:id="@+id/outletStockCapacity"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text="@={String.valueOf(outlet.capacity)}"
android:singleLine="true"
android:imeOptions="actionNext"/>


The first was working fine on
Post Lollipop
but, not working on
Pre-Lollipop
. This is my SimpleTextWatcher

public abstract class SimpleTextWatcher implements TextWatcher {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void afterTextChanged(Editable s) {
onTextChanged(s.toString());
}

public abstract void onTextChanged(String newValue);
}


My project wouldn't build because of this. I don't know why? I checked for null, initialized it and all. But, it wouldn't just build because of that

Answer

Try this android:text="@={outlet.capacityAsString}", where capacityAsString is String (ObservableField<String>) I dont think, you can use something like String.valueOf(outlet.capacity) for two way binding.