2016-07-25 27 views
0

in EditExt Beobachtete immer habe ich eine ModellklasseObservableArrayMap mit Objekt als Wert nicht auf Wertänderung

public class ScopeDefination extends BaseObservable { 

String value; 
String key; 
boolean isPrivate; 
String category; 

public String getCategory() { 
    return category; 
} 

public void setCategory(String category) { 
    this.category = category; 
} 

public ScopeDefination(String key, String value, boolean isPrivate) { 
    this.key = key; 
    this.value = value; 
    this.isPrivate = isPrivate; 
} 

public ScopeDefination(String key, String value, boolean isPrivate, String category) { 
    this.key = key; 
    this.value = value; 
    this.isPrivate = isPrivate; 
    this.category = category; 
} 

@Bindable 
public String getKey() { 
    return key; 
} 

public void setKey(String key) { 
    this.key = key;notifyPropertyChanged(developer.manish.publicprivatescopecategory.BR.key); 
} 

@Bindable 
public String getValue() { 
    return value; 
} 

public void setValue(String value) { 
    this.value = value; 
    notifyPropertyChanged(developer.manish.publicprivatescopecategory.BR.value); 
} 

public boolean isPrivate() { 
    return isPrivate; 
} 

public void setPrivate(boolean aPrivate) { 
    isPrivate = aPrivate; 
} 

}

und ich versuche, es in einer Klasse wie diese

Bindung für Daten zu verwenden,
ScopeDefination scopeDefination1 = new ScopeDefination(Keys.ADDRESS_HOME,"Address", true, Keys.FINANCIAL_INFO); 
     ScopeDefination scopeDefination2 = new ScopeDefination(Keys.NAME,"Name", true, Keys.FINANCIAL_INFO); 

     ObservableArrayMap<String, Object> user = new ObservableArrayMap<>(); 
     user.put(Keys.ADDRESS_HOME, scopeDefination1); 
     user.put(Keys.NAME, scopeDefination2); 
fragmentFirebaseBinderBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_firebase_binder, container, false); 
     fragmentFirebaseBinderBinding.setUser(user); 

Meine XML ist

<?xml version="1.0" encoding="utf-8"?> 

<data> 

    <import type="developer.manish.publicprivatescopecategory.ScopeDefination" /> 

    <import type="android.databinding.ObservableMap" /> 

    <variable 
     name="user" 
     type="ObservableMap&lt;String, Object>" /> 
</data> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="developer.manish.publicprivatescopecategory.FirebaseBinder"> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text='@{((ScopeDefination)user["username"]).getValue()}'/> 

    <TextView 
     android:id="@+id/address" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text='@{((ScopeDefination)user["address_home"]).getValue()}' /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:text='@{((ScopeDefination)user["username"]).getValue()}' /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:text='@{((ScopeDefination)user["address_home"]).getValue()}' /> 

</LinearLayout> 

Ich bin der Lage, Daten auf UI zu zeigen, aber wenn ich die entsprechende Textansicht immer aktualisiert keine Daten in EditExt bearbeiten.

Antwort

0

Was Sie wollen, ist ein Zwei-Wege-Bindung, die etwas anders als die normale Bindung ist. Siehe die blog entry von George Mount.

Grundsätzlich müssen Sie Ihre Bindung Ausdrücke von

android:text='@{((ScopeDefination)user["username"]).getValue()}' 

zu

android:text='@={((ScopeDefination)user["username"]).value}' 

Hinweis bearbeiten, dass es eine zusätzliche = zwischen dem @ und dem {. Dadurch wird Ihr Modell benachrichtigt, sobald sich die Daten in EditText geändert haben. Außerdem können Sie keine Methoden in verwenden, aber die Verwendung von .value funktioniert. Danke an @George Mount, um darauf hinzuweisen.

+0

Fehler: (26, 27) Kein Ressourcentyp angegeben (bei 'text' mit Wert '@ = {((ScopeDefination) user ["Benutzername"]). GetValue()}'). –

+0

bekommen diesen Fehler danach –

+0

Sieht aus wie ein Tippfehler. Überprüfen Sie das. – yennsarah