android - Android - 在多个 RootView 中获取对象
问题描述
public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
如果我的术语在标题中是正确的,但这是我正在做的事情......当我点击“添加更多设备”时,它会向我的应用程序添加布局,所以我想在我添加到 RootView 的布局中获取所有 EditText,我尝试设置文本来测试它,但第一个 rootView 的第一个 editText 是唯一更新的。上面列出的我的片段中的所有代码。附加图像是此代码的结果。
解决方案
我已经按照@hjchin 的建议使用 *RecyclerView 完成了这项任务。谢谢你!
推荐阅读
- python - 在 Python 中对 x,y 网格执行计算
- visual-studio-code - 是否可以在 VSC 数据查看器中更改“NaN”的颜色?
- vue.js - 将litepie-datepicker导入VueJS项目时“未捕获的TypeError:Object(...)不是函数”
- kotlin - kotlin 数据表结构,用于保存和排序数据
- php - 是否可以使用 glob 模式来匹配 zip 内容?
- microsoft-graph-api - MS Graph 事件 - 在日历之间移动事件时保留自定义属性
- php - 如何使用 OAuth 协议连接到 Google My Business API?
- javascript - 从字符串数组中,使用组件以不同的样式呈现字符串数组的某些索引
- reactjs - 为什么 React 组件在状态不变的情况下只重新渲染一次?
- typescript - 根据环境的动态角色