3

In my app I have an ElementListFragment of items represented by a code object Element. I've data-bound these elements to the list and they display the correct information. However, to continue filling in the information in each item I put a button on each element that shows a Dialog with additional fields. But when the Dialog opens the fields are all blank (where one at least ought to be filled) and any fields I fill don't save the data (and don't effect changes on the list).

Appart from the values not being bound (displayed or written), the application works fine. I've essentially tried multiple variations of the sugestions in this question. Here is the code:

public class ElementDialogFragment extends DialogFragment {
        private Element mElement;
        private Dialog dialog;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View v = inflater.inflate(R.layout.dialog_element, null);

            final DialogSkillelementBinding binding = DialogElementBinding.inflate(LayoutInflater.from(getContext()));

            builder.setView(v)
                    // Add action buttons
                    .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            //this was just an attempt to make it work
                            binding.executePendingBindings();
                            dialog.dismiss();
                        }
                    });

            TextView Title = v.findViewById(R.id.skill_e_dialog_title);
            Title.setText(ResourceLocator.getSkillName(mElement.getSkill()));

            dialog = builder.create();
            dialog.setContentView(binding.getRoot());

            binding.setElement(mElement);
            binding.executePendingBindings();

            return dialog;
        }
    }

When debuging I've confirmed that mElement is correctly attributed, however it's also bound to the list shown beneath the dialog. The title is also displayed correctly.

Is there a problem double-binding an object?

Are some of the steps on the function maybe out of order?

Is the DialogBuilder somehow incompatible with databinding?

1 Answer 1

1

You should set LyfecycleOwner for your binding class for data updating.

binding.setLifecycleOwner(requireActivity())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.