如何在Android中将双向数据绑定用于动态创建的字段 [英] How to use two-way data binding in Android for dynamically created fields
问题描述
我有一个表单,其字段由服务器返回值设置.
I have a form whose fields are set by server return.
这是活动中的代码.
//returned by server
String[] attributes = new String[] {"occupation", "salary", "age"};
LinearLayout dynamicLayout = (LinearLayout) findViewById(R.id.dynamic_layout);
for (String attribute : attributes) {
EditText text = new EditText(this);
text.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
text.setText(attribute);
dynamicLayout.addView(text);
}
我有这个模型课
class Person {
public Map<String, String> attributes;
}
最后,我希望上面的属性将包含属性名称和EditText
In the end I expect the attributes above will contain the map between attribute name and the values input in the EditText
我尝试创建一个双向数据绑定示例,该示例使用布局文件中预定义的EditText.但是我想知道这个动态属性是否可以实现.
I have tried created a two-way data-binding sample that uses EditText that is predefined in the layout file. But I am wondering if this dynamic attribute can be done.
推荐答案
数据绑定实际上不适用于代码生成的布局.就是说,您可以使用数据绑定和绑定适配器来完成此操作.
Data Binding doesn't really work with code-generated layouts. That said, you can use data binding to do this with a Binding Adapter.
本文告诉您如何对列表使用数据绑定:
This article tells you how to use data binding with lists:
https://medium.com/google-developers/android-data-binding-list-tricks-ef3d5630555e#.v2deebpgv
如果要使用数组而不是列表,这是一个很小的调整.如果要使用地图,则必须将地图作为参数传递给绑定布局.本文假定使用一个变量,但是您可以在BindingAdapter中传递多个变量.对于一个简单的绑定适配器:
If you want to use an array instead of a list, it is a small adjustment. If you want to use a map, you'll have to pass the map as a parameter to the bound layout. The article assumes a single variable, but you can pass in multiple in your BindingAdapter. For a simple Binding Adapter:
@BindingAdapter({"entries", "layout", "extra"})
public static <T, V> void setEntries(ViewGroup viewGroup,
T[] entries, int layoutId,
Object extra) {
viewGroup.removeAllViews();
if (entries != null) {
LayoutInflater inflater = (LayoutInflater)
viewGroup.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < entries.length; i++) {
T entry = entries[i];
ViewDataBinding binding = DataBindingUtil
.inflate(inflater, layoutId, viewGroup, true);
binding.setVariable(BR.data, entry);
binding.setVariable(BR.extra, extra);
}
}
}
您将这样绑定条目:(假设entry.xml)
And you would bind the entries like this: (assume entry.xml)
<layout>
<data>
<variable name="data" type="String"/>
<variable name="extra" type="java.util.Map<String, String>"/>
</data>
<EditText xmlns:android="..."
android:text="@={extra[data]}"
.../>
</layout>
在您的包含布局中,您将使用:
And in your containing layout, you'd use:
<layout>
<data>
<variable name="person" type="com.example.Person"/>
<variable name="attributes" type="String[]"/>
</data>
<FrameLayout xmlns:android="..." xmlns:app="...">
<!-- ... -->
<LinearLayout ...
app:layout="@{@layout/entry}"
app:entries="@{attributes}"
app:extra="@{person.attributes}"/>
</FrameLayout>
</layout>
这篇关于如何在Android中将双向数据绑定用于动态创建的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!