首页 > 解决方案 > 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 是唯一更新的。上面列出的我的片段中的所有代码。附加图像是此代码的结果。

图像结果

标签: androidrootview

解决方案


我已经按照@hjchin 的建议使用 *RecyclerView 完成了这项任务。谢谢你!

RecyclerView 教程@Developer.Android


推荐阅读