Untitled
unknown
plain_text
a month ago
1.7 kB
2
Indexable
val dynamicMultiChoiceItems = listOf("Option 1", "Option 2", "Option 3", "Option 4") // Dynamic multi-choice options val multiChoiceSelected = BooleanArray(dynamicMultiChoiceItems.size) // Track selections (false by default) var selectedSingleChoiceIndex = -1 // Initially, no single choice is selected // Build the dialog val builder = AlertDialog.Builder(this) builder.setTitle("Choose options") // Inflate the custom layout val inflater = layoutInflater val view = inflater.inflate(R.layout.dialog_multiple_choices, null) builder.setView(view) // Set up the RecyclerView for both multi-choice and single-choice options val recyclerView = view.findViewById<RecyclerView>(R.id.multiChoiceRecyclerView) val items = dynamicMultiChoiceItems + "Single Option" // Combine multi-choice items and single choice val adapter = ChoiceAdapter(items, selectedSingleChoiceIndex, multiChoiceSelected) { position -> // Handle single choice selection selectedSingleChoiceIndex = position // Notify adapter to update selection state adapter.notifyDataSetChanged() } recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = adapter // Positive Button to confirm selections builder.setPositiveButton("OK") { _, _ -> // Handle OK click (retrieve the selected multi-choice and single-choice items) dynamicMultiChoiceItems.forEachIndexed { index, item -> Log.d("MultiChoice", "$item is ${if (multiChoiceSelected[index]) "checked" else "unchecked"}") } if (selectedSingleChoiceIndex >= 0) { Log.d("SingleChoice", "Single Choice: ${items[selectedSingleChoiceIndex]}") } } // Negative Button to cancel builder.setNegativeButton("Cancel", null) // Show the dialog builder.create().show()
Editor is loading...
Leave a Comment