0

I am trying to keep the state of my checkboxes on checked if there were any checkboxes left which were selected.

I have the following dialog for it:

fun showMultipleChoiceDialog(options: Array<String>?, listener: DialogInterface.OnMultiChoiceClickListener) {
        if (options != null && options.isNotEmpty()) {
            val dialog = AlertDialog.Builder(SharedInstance.instance.context)
            .setMultiChoiceItems(options, null, listener)
            .setPositiveButton("Ok", DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() })
            .create()
            dialog.show()
        }
    }

And I have the following code already:

days_picker.setOnClickListener {
            val allDays = ArrayList(days.values)
            val allStringDays = arrayOf("Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag")
            var checkedItems: MutableList<String> = mutableListOf()
            SearchProfileDialogHelper.selectedDays.clear()
            searchProfile.days.clear()

            DialogHelper.showMultipleChoiceDialog(allStringDays, DialogInterface.OnMultiChoiceClickListener { _, which, isChecked ->
                if(isChecked){
                    SearchProfileDialogHelper.selectedDays.add(allDays[which])
                    checkedItems.add(allDays[which])
                }
                if(!isChecked){
                    SearchProfileDialogHelper.selectedDays.remove(allDays[which])
                    checkedItems.remove(allDays[which])
                    if(checkedItems.size == 0) {
                        days_text.setText("Stel in voor welke dagen u beschikbaar bent.")
                    }
                }
}

Right now when I select checkboxes it works fine. If I unselect a checked checkbox it works fine as well. But what I still want to include in my project is being able to press the OK button without selecting anything and keeping the default text if there was a selection made before that. (Otherwise it already shows the default text) and most importantly I want to save the state of which checkboxes were checked until the user changes this again. So whenever I re-open my dialog I want to see what the user had checked the last time.

My problem is that I can not seem to find out how to call a checkbox and set it to a certain state. I am already saving the checkeditems in a list so that's not the issue here. The helper function showMultipleChoiceDialog is from one of my colleagues hence why I am having trouble understanding how to let the state remain the same before anyone changes anything.

So I am hoping there are people who can help me out!

I already checked the following topics on stack:

And several youtube video's already but most of them work with stuff like findViewById and are working in activity's while I am working in fragments.

sanjeev
  • 1,664
  • 19
  • 35
  • `var checkedItems: MutableList = mutableListOf()` At restart of your app you should set those items as they were at last time closing. Then go to your coworker and tell him to change his function from `.showMultipleChoiceDialog(allStringDays, DialogInterface.On...` to `.showMultipleChoiceDialog(allStringDays, checkedItems, DialogInterface.On...`. – blackapps Dec 13 '19 at 13:21
  • Thanks, I already had this but this works indeed. Only problem I have now is that it has a delay once I press the "OK" button after changing amount of checkboxes checked. But that should be pretty easy to fix I suppose, probably with a completionhandler added to the multiplechoicedialog as well, but preferably without if possible! – frankhardenberg Dec 16 '19 at 07:51

1 Answers1

1
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("Checkbox" + checkbox_number, checked).apply();

Where checkbox_number is just counter of CheckBoxes (0,1,2,3...) Then you just read checked values like this:

ArrayList<Boolean> checkedList = new ArrayList<>();
for (int i = 0; i < check_box_count; i++) {
     checkedList.add(PreferenceManager.getDefaultSharedPreferences(this).getInt("CheckBox" + i, false);
}

I'm not sure if this is what you are asking about but I use this approach everytime i need to save state of CheckBoxes. If I'm dealing with multiple CheckBoxes at a time I use their Id instead of counter so I don't have to worry how to deal with more than one at a time (for example multichoice at different pages of ViewPager)

I'm sorry it's in Java but I prefer to speak in my language. Yet I'm newbie at Kotlin.

SkypeDogg
  • 1,000
  • 2
  • 13
  • 28
  • Unfortunately this is not what I am looking for. I don't have separate checkboxes, I have a dialog so I can't just say checkbox1.isenabled or something. Wish it was as easy as that. I'm forced to learn to work with the helper methods my employer gives me so I'll need to work it out somehow. – frankhardenberg Dec 13 '19 at 12:56