Home > Enterprise >  Adding a single choice alert dialog into a recycler view
Adding a single choice alert dialog into a recycler view

Time:08-30

I am new to alert dialogs and was hoping somebody could help me with this. I want to develop a single choice alert dialog and have it show in a recyclerview textview along side an incremental counter. I have searched all types of documentation but all I can find is how to display the single choice item in either a Toast or a single text view. I know the code I have is incorrect, but after numerous other attempts, this is the closest I got to getting the result I am seeking. I was able to get it to set the most recent choice but then the other choices change into what look like memory allocations after the button is pressed.

Screenshot: enter image description here

Here is my code: Main Activity (I realize that tv_choice.setText(multiItems[i]) is part of the problem it in my dialogAlert(). This is what I need help with.

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val itemsList = generateItemsList()
private val adapter = MyAdapter(itemsList)
var count = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.setHasFixedSize(false)
    binding.button.setOnClickListener {
        addItemsToRecyclerView()
    }
}

private fun generateItemsList(): ArrayList<Items> {
    return ArrayList()
}
fun addItemsToRecyclerView() {
    val addItems = Items(getCount(), "Your Choice Is:", dialogAlert())
    itemsList.add(addItems)
    adapter.notifyItemInserted(itemsList.size - 1)
}
private fun getCount(): String {
    count  = 1
    return count.toString()
}
fun dialogAlert(): String {
    val multiItems = arrayOf("Item 1", "Item 2", "Item 3")
    val singleChoice = AlertDialog.Builder(this)
        .setTitle("Choose one:")
        .setSingleChoiceItems(multiItems, 1) { dialogInterface, i ->
            tv_choice.setText(multiItems[i])

        }
        .setPositiveButton("ok") { _, _ ->
        }
        .create()
    singleChoice.show()
    val singleChoiceString = singleChoice.toString()
    return singleChoiceString
}

}

The Adapter:

class MyAdapter(private val rvDisplay: MutableList<Items>) : RecyclerView

.Adapter<MyAdapter.AdapterViewHolder>(){

class AdapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val textView1: TextView = itemView.findViewById(R.id.tv_count)
    val textView2: TextView = itemView.findViewById(R.id.tv_choice_string)
    val textView3: TextView = itemView.findViewById(R.id.tv_choice)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterViewHolder {
    val myItemView = LayoutInflater.from(parent.context).inflate(
        R.layout.rv_items,
        parent, false
    )
    return AdapterViewHolder(myItemView)
}

override fun onBindViewHolder(holder: MyAdapter.AdapterViewHolder, position: Int) {
    val currentDisplay = rvDisplay[position]
    holder.itemView.apply {
        holder.textView1.text = currentDisplay.count
        holder.textView2.text = currentDisplay.choiceString
        holder.textView3.text = currentDisplay.choice
    }
}

override fun getItemCount() = rvDisplay.size
}

CodePudding user response:

While you tried to add the dialog's selected value to the recycler view, what you actually did was adding the dialogAlert() returned value to the recycler view.

Instead "adding" an item when the button is clicked, you should add the item once the dialog is closed. So first present the dialog:

binding.button.setOnClickListener {
    dialogAlert()
}

Remove the return value from dialogAlert() method and then, when selecting an option from the dialog, add it to the recycler view:

fun dialogAlert() {
    val multiItems = arrayOf("Item 1", "Item 2", "Item 3")
    val singleChoice = AlertDialog.Builder(this)
        .setTitle("Choose one:")
        .setSingleChoiceItems(multiItems, 1) { dialogInterface, i ->
            addItemsToRecyclerView(multiItems[i])
        }
        .create()
    singleChoice.show()
}

Change the method to receive a String (your item):

fun addItemsToRecyclerView(item: String) {
    val addItems = Items(getCount(), "Your Choice Is:", item)
    itemsList.add(addItems)
    adapter.notifyItemInserted(itemsList.size - 1)
}

Note that I did not run this code so it might need some adjustments.

  • Related