[英]Custom setter error in data binding
部分布局:
<data>
<import type="android.view.View"></import>
<variable
name="tags"
type="java.util.HashMap"></variable>
</data>
查看xml:
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/overview_customerHashtags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexWrap="wrap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/overview_tokenTimeout"
app:tags="@{tags}">
</com.google.android.flexbox.FlexboxLayout>
我的綁定適配器:
@BindingAdapter("tags")
public static void setTags(FlexboxLayout flexboxLayout, HashMap<Integer, HashtagModel> hashtags) {
for (HashtagModel hashtagModel : hashtags.values()) {
flexboxLayout.removeAllViews();
HashtagButton button = new HashtagButton(flexboxLayout.getContext(), hashtagModel.getCategory());
int resId = flexboxLayout.getContext().getResources().getIdentifier(hashtagModel.getTitle().toUpperCase(), "string", flexboxLayout.getContext().getPackageName());
if (resId == 0) {
button.setText(hashtagModel.getDefault_text());
} else {
button.setText(flexboxLayout.getContext().getResources().getString(resId));
}
flexboxLayout.addView(button);
}
}
它不斷給出此錯誤:
錯誤:(162,29)在com.google.android.flexbox.FlexboxLayout上找不到參數類型為java.util.HashMap的屬性“ app:tags”的設置器。
有什么建議嗎? 謝謝。
如果使用HashMap
綁定到xml視圖。 實現看起來像這樣
為HashMap
getter和setter方法創建模型:
public HashMap<String, String> thing = new HashMap<String, String>() {{
put("tag 1", "tag 2");
}};
public HashMap<String, String> getTag() {
return thing;
}
public void setTag(HashMap<String, String> tag) {
this.thing = thing;
}
里面的xml:
<data>
<import type="android.view.View"></import>
<variable
name="thing"
type="java.util.HashMap"></variable>
</data>
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/overview_customerHashtags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexWrap="wrap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/overview_tokenTimeout"
app:tags="@{thing["tag 1"]}">
</com.google.android.flexbox.FlexboxLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.