Home » Java » the same view with different logic in android

the same view with different logic in android

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’ve seen this mentioned a few times but not resolved.
In android studio, i use a xml form to enter the information for an object (title, date, cost etc)

I want to use the exact same view for “edit object” and “add object”, with the only difference being that if you’re editing, the “done” button updates something in the db, and if you’re adding, it creates a new object for the database.

It doesn’t seem to make sense to have the exact same code, linking the view elements (like editText) to onChange listeners that store the inputs etc, with the only difference being what happens when the user presses “DONE”

The way I’ve handled it is by passing an intent to the activity with the form view, that has a Boolean specifying whether we are adding or editing, but this seems a hacky and insufficient method.

Am i missing something? is there some way to not use this hacky boolean and not reuse all that code?

Answers: