Ich habe Kontrollkästchen, Edittexte und andere Eingaben in meinem Recyclerview. Ich habe Databinding erfolgreich implementiert, was bedeutet, dass Daten aus den Daten, die ich vom Webservice erhalte, aufgefüllt werden. Wenn ich zB ein Eingabefehler ändere, ändere den Wert 104 auf 107 und scrolle nach unten (so dass EditText) jetzt länger im Ansichtsfenster ist und blättern zurück, ich bekomme den Wert 104 wieder. Es scheint, dass RecyclerView den alten Wert zwischenspeichert und meine Datenbindungsbibliothek das Modell nicht aktualisiert (Ja, ich benutze Bindable-Attribute und andere notwendige Dinge), und ich benutze auch die Methode executePendingBindings im Adapter.android Databinding innerhalb Recyclerview
Das ist mein RecyclerView Adapter:
public class SimpleAttributesAdapter extends RecyclerView.Adapter<SimpleAttributesAdapter.AttributesViewHolder> {
private final Context context;
private final List<Attribut> allAttributes;
public SimpleAttributesAdapter(Context context,List<Attribut> _allAttributes)
{
this.context=context;
this.allAttributes=_allAttributes;
}
@Override
public AttributesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(context);
RecyclerViewAttributesChildOneRowBinding binding= DataBindingUtil.inflate(inflater, R.layout.recycler_view_attributes_child_one_row, parent, false);
MyHandlers handlers = new MyHandlers();
binding.setHandlers(handlers);
return new AttributesViewHolder(binding);
}
@Override
public void onBindViewHolder(AttributesViewHolder holder, int position) {
RecyclerViewAttributesChildOneRowBinding binding=holder.getViewDataBinding();
binding.setAttribute(allAttributes.get(position));
holder.getViewDataBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return allAttributes.size();
}
public class AttributesViewHolder extends RecyclerView.ViewHolder {
private RecyclerViewAttributesChildOneRowBinding binding;
public AttributesViewHolder(RecyclerViewAttributesChildOneRowBinding viewDataBinding)
{
super(viewDataBinding.getRoot());
binding=viewDataBinding;
binding.executePendingBindings();
}
public RecyclerViewAttributesChildOneRowBinding getViewDataBinding() {
return binding;
}
}
}
Das ist mein Layout-Datei ist:
< ?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="com.adriagate.adriagateonlineandroid.models.Attribut"/>
<variable
name="attribute"
type="com.adriagate.adriagateonlineandroid.models.Attribut" />
<import type="android.view.View"/>
<variable name="handlers"
type="com.adriagate.adriagateonlineandroid.MyHandlers"/>
</data>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{attribute.Question}"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@{attribute.Answer}"
android:inputType="number"
bind:visibility="@{attribute.isNumber ? View.VISIBLE : View.GONE}"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@{attribute.Answer}"
android:inputType="text"
bind:visibility="@{attribute.isString ? View.VISIBLE : View.GONE}"
/>
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:adriagateentries="@{attribute.Choices}"
bind:visibility="@{attribute.isSelect ? View.VISIBLE : View.GONE}"
bind:selectedId="@{attribute.ChoiceId}"
style="@style/Widget.AppCompat.Spinner.Underlined"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:checked="@{attribute.Selected}"
bind:visibility="@{attribute.isBool ? View.VISIBLE : View.GONE}"/>
</LinearLayout>
</layout>
Das ist mein Modell ist:
public class Attribute extends BaseObservable {
@Bindable
public String getAnswer() {
return Answer;
}
public void setAnswer(String answer) {
Answer = answer;
notifyPropertyChanged(BR.answer);
}
public List<Choice> getChoices() {
return Choices;
}
public void setChoices(List<Choice> choices) {
Choices = choices;
}
public int getType() {
return Type;
}
public void setTypeype(int attributeType) {
Type = attributeType;
}
@Bindable
public String getChoiceId() {
return ChoiceId;
}
public void setChoiceId(String choiceId) {
ChoiceId = choiceId;
notifyPropertyChanged(BR.choiceId);
}
@Bindable
private String Answer; //tipovi 1
@Bindable
private String ChoiceId; //tipovi 1
private List<Choice> Choices; //za tip 3
private int Type;
public boolean isNumber() {
return this.isNumber = this.getType()==1;
}
private boolean isNumber ;
public boolean isSelect() {
return this.isSelect = this.getType()==3;
}
private boolean isSelect ;
public boolean isString() {
return this.isString = this.getType()==2;
}
private boolean isString ;
private boolean isBool ;
public boolean isBool() {
return this.isBool = this.getType()==5;
}
private boolean isChecked;
public boolean isChecked() {
return this.Answer =="D";
}
}
Wo ist der Code, der die Änderungen an dem Bearbeiten von Text und wendet sie auf Ihr Datenmodell nimmt? –
Benutzer verwendet App und ändert Wert innerhalb EditText, Zwei-Wege-Datenbindungsbibliothek aktualisiert das Modell. Es sollte so funktionieren. Zwei-Wege-Datenbindungsbibliotheken in der Web-Welt wie angularjs funktionieren in welcher Weise und so sollte Datenbindung funktionieren. –
1) Ich sehe den Code nicht, der die Änderung in dem Bearbeitungstext übernimmt und es an dem Modell festhält. Ich sehe nichts auf der Android-Datenbindungsseite, die irgendeinen magischen Kleber beschreibt, der das für dich tun wird. 2) Ich sehe nicht, dass Sie den Notifier aufrufen, wenn sich das Datenmodell ändert. –