pratikbutani / MultiSelectSpinner

Android - Select Multiple Items from Spinner with Filtration.
http://pratikbutani.github.io/MultiSelectSpinner/
GNU General Public License v3.0
253 stars 125 forks source link

MultiSpinnerSearch does not show when created programatically #105

Closed petered closed 2 years ago

petered commented 2 years ago

First of all, thanks for creating this excellent tool.

I'm trying to get it to work in an alert dialog (basically as a mini-form that you fill out and click "ok"). To do this, I create the MultiSpinnerSearch programatically, as below, but it does not display. (It does display correctly when create within the Activity's XML file).

val allSources = listOf("manual", "auto")
val spinner = MultiSpinnerSearch(applicationContext).apply{
    title="Source"
    setItems(allSources.map{ KeyPairBoolData(it, false) }){}
}
val exampleText = Button(applicationContext).apply{text="Example"; setOnClickListener{showToast("Button Pressed")}}
val form = LinearLayout(applicationContext, ).apply{
    orientation=LinearLayout.VERTICAL;
    addView(spinner);
    addView(exampleText)
}
AlertDialog.Builder(this)
    .setView(form)
    .setPositiveButton("Filter") { _, _ ->
        updateDetectionDisplayAsync()
        showToast("Updated detections")
    }
    .setNegativeButton("Cancel") { _, _ -> }
    .create().show()

image

I believe it's to do with the fact that building programatically uses the MultiSpinnerSearch(context) constructor instead of the MultiSpinnerSearch(context, attributes) constructor used when building from xml. I tried constructing fake attributes to construct the other way but ran into a dead end there where constructing a valid fake attributes object seemed complicated.

I'm not sure whether the solution to this involves modifying the source code of MultiSelectSpinner - any pointers appreciated and if it's not too involved I could possibly figure it out and make a PR with the fix.

petered commented 2 years ago

Ah - figured it out - if you construct with MultiSpinnerSearch(this@ActivityName) instead of MultiSpinnerSearch(applicationContext), it works

pratikbutani commented 2 years ago

Thank for your time @petered