How to update edittext whenever a quantity is added in android studio

I will disappoint you by not sharing code. I will share you a simple graphic of what you need to do.

  • [Fragment]: displays your list of orders. When an event is triggered (user tapped something or changed something), forwards this event to a ViewModel. Also Observes for the ViewModel data state.

  • [ViewModel]: exposes a series of functions to listen for user events (e.g. the user tapped this order, or the user changed the quantity of this order to NN, etc.). Also offers a LiveData (or Flow) with the State and Data your Fragment needs. Maybe even talks to the repository where this data originates from (where you save your Orders in your persistent or backend).

Interactions:

When the UI modified something, you tell the viewModel: “This data changed, the order X, now has a quantity of Y”.
The ViewModel will process this, update the State of the screen, and send the new data via your Livedata/Flow that the Fragment observes.
When the Fragment receives this new data, it will update the UI accordingly, as that’s its most important job.

This whole thing has been greatly documented in the official Android recommended architecture guides. I suggest you start from there instead of trying to fight the framework and making your life more complicated.

Leave a Comment