Home > Blockchain >  In Android Studio (Kotlin & XML), how would one integrate switches into individual cards in a Recycl
In Android Studio (Kotlin & XML), how would one integrate switches into individual cards in a Recycl

Time:12-21

I'm trying to make a medical reminder app with a menu screen where you can select a desired category of treatments (image with Home at the top). From there, individual treatments can be selected by turning on the switch for each treatment (image with Cataract Surgery). Once the user has selected their treatments, they can click on the "medical bag" icon and then see a summary of their treatments that they selected (image with Summary).

The problem I am having is figuring out how to implement it so that when a switch is turned on (for an individual treatment), it makes a new card on the summary screen with its name and corresponding color. The goal of the summary screen is to collect all of the treatments that were selected and allow the user to indicate the frequency of the individual treatments.

If my situation sounds confusing, here is the link to my project on GitHub.

https://github.com/milingupta/medminder-android

Image of Home Menu

Image of Treatment Menu

Image of Summary Menu

I am very confused and don't really know where to start implementing this functionality. If anyone has any thoughts, I'd really appreciate it. Please see my description of the problem for more details and view my code in the link to GitHub.

CodePudding user response:

I think you need to implement Intent(this,???::class.java), intent.putExtra(), intent.get, setContentView(R.layout.???) in your code

CodePudding user response:

Here are the steps you should take:

  1. You dataset in your adapter is static doesn't contain the state of the switch. Convert the dataset to a model so it contains the state of the switch as well. For example List<Pair<CataractTreatment,Boolean>>.
  2. Add a method to your adapter to update the dataset. This method should trigger a DiffUtil or call notifyDatasetChanged.
  3. Add a lamda parameter to the constructor of your adapter. private val onSwitchCheckChanged: (Boolean) -> Unit. And in the onBindViewModel you should add listener to the switch which should cal the onSwitchCheckChanged method when toggling the switch.
  4. And finally add a method in your activity that updated the dataset and updates the state of the adapter.

Here is google sample which implements an Adapter with an onClick listener. This should be similar and should help you with implementing the steps above https://github.com/android/views-widgets-samples/blob/main/RecyclerViewKotlin/app/src/main/java/com/example/recyclersample/flowerList/FlowersAdapter.kt

A few extra tips based on your code base:

  • Start using databinding, this makes findViewById obsolete
  • Start using ViewModels.
  • Related