Android对话框自行更改Activity变量

时间:2019-12-04 12:05:07

标签: android dialog state

我在Activity form中有一个Android,用户可以填写,基本上是一些基本的EditText字段。然后可以选择向表单中添加新的Device,以便结构看起来像这样:

FormData{
    String name;
    List<Device> devices;
}

Device{
    String id;
    List<Accessory> accessories;
}

Accessory{
    String id;
    boolean isChecked;
}

当用户单击addNewDevice时,将显示dialog,用户可以在其中看到devices的列表。当用户在该Dialog中的设备上进行选择时,将显示一个新的Dialog,用户可以在其中填充该Device的数据并单击addDevice

一旦添加了Device,用户就可以在Activity中以recyclerView看到初始形式的地方看到它。然后,用户可以选择添加更多Devices(如先前所述)或单击device中先前添加的recyclerView,然后查看之前填写的内容并进行编辑如果用户愿意。

Activity中有2个列表:

List<Device> devices;
List<Device> addedDevices;

devices列表是通过从后端获取数据来填充的,在初始请求之后,它们应保持不变。然后,此列表用于显示在Dialog中,用户可以在其中选择要开始填写并添加到表单的不同devices

填写好Device之后(选择了addDevice按钮后,从Device列表devices中选择他想要填写的and filling the设备form ) the设备is then added to the list添加了设备and displayed in a recyclerView`。

选择addedDevices之一后,它会再次显示在Dialog中,用户可以在其中进行填写和编辑。

这里的问题是,一旦用户将第一个对话框添加到addedDevices列表中,似乎就保存了Dialog状态,并由于某种原因更改了devices列表,并填充了输出数据。

因此,当我再次单击addDevice按钮时,它已保存了我填写的上一个表格中的旧数据。通过检查日志,我发现devices列表已更改。

MainActivity

    private List<InstallationDocumentDevice> installationDocumentDevices;
    private List<InstallationDocumentDevice> devicesToAdd;     

    @OnClick(R.id.btnAddDevice)
    void onAddDevice() {
    Log.d(TAG, "size: " + installationDocumentDevices.size());

    InstallationDocumentDeviceSelectDialog dialog = new InstallationDocumentDeviceSelectDialog(context, installationDocumentDevices, new InstallationDocumentDeviceSelectDialog.OnItemClickListener() {
        @Override
        public void onItemClick(InstallationDocumentDevice installationDocumentDevice) {
            Log.d(TAG, "devicey: " + installationDocumentDevice.toString());
            Log.d(TAG, "installationdocdevices to add" + installationDocumentDevices.toString());
            onAddDeviceSelected(installationDocumentDevice);

        }
    });
    dialog.show();

    Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    WindowManager.LayoutParams wlp = window.getAttributes();
}

    private void onAddDeviceSelected(InstallationDocumentDevice installationDocumentDevice) {
    InstallationDocumentDeviceAddFormDialog dialog = new InstallationDocumentDeviceAddFormDialog(context, installationDocumentDevice, new InstallationDocumentDeviceAddFormDialog.OnItemClickListener() {
        @Override
        public void onItemClick(InstallationDocumentDevice installationDocumentDevice) {
            devicesToAdd.add(installationDocumentDevice);
            addedDevicesListAdapter.setItems(devicesToAdd);
        }
    });
    dialog.show();

    Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.BOTTOM;
    window.setAttributes(wlp);
}


private void initAddedDevicesList() {
    View.OnClickListener onClickListener = clickedView -> {
        InstallationDocumentDevice installationDocument = (InstallationDocumentDevice) clickedView.getTag();
        Log.d(TAG, "dev to add" + devicesToAdd.toString());
        Log.d(TAG, "installdoc" + installationDocument.toString());
        InstallationDocumentDeviceAddFormDialog dialog = new InstallationDocumentDeviceAddFormDialog(context, installationDocument, new InstallationDocumentDeviceAddFormDialog.OnItemClickListener() {
            @Override
            public void onItemClick(InstallationDocumentDevice installationDocumentDevice) {

            }
        });
        dialog.show();

        Window window = dialog.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        WindowManager.LayoutParams wlp = window.getAttributes();

        wlp.gravity = Gravity.BOTTOM;
        wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);
    };

    addedDevicesListAdapter = new AddedDevicesListAdapter(context, onClickListener);
    addedDevicesRecyclerView.setHasFixedSize(true);
    addedDevicesRecyclerView.setAdapter(addedDevicesListAdapter);
    addedDevicesRecyclerView.addItemDecoration(new DividerItemDecoration(addedDevicesRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
}

Dialog片段中,我不访问MainActiviy来更改其中的任何内容。只需将变量传递到Dialog中,然后onclick将其传递回来,如使用onClick方法所看到的那样。

在任何时候打开Dialog时..无论是添加新设备还是更改设备,对话框似乎只记住Dialog的最后状态,并且不会加载一个新的。

0 个答案:

没有答案