0

I am not adding any code since its just a simple recyclerview that I want to work with. So the recyclerview is inside a fragment and I am adding a some radio buttons. I want to make two of them selectable.

I have set up everything with a simple adapter using a model class that holds the answer and the answerID.

I have two interfaces which I want to use to send the answer to the activity. I am stuck with the logic of how I should do it. Or is there a way to make a radio group with two selectable radio buttons.

I am looking for suggestions on how to structure the code.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pemba Tamang
  • 458
  • 3
  • 16
  • I'm not sure why I haven't tested the code. However, group all together in a radiogroup. Set the click where you take the reference to the radiogroup. Also add the reference to your interface (check if the activity implements the interface, in the onAttach method). In the onClick method call the method through the interface). – RaffaD Oct 31 '19 at 16:44
  • Do you mean that you want to send data to the fragment when you click radio buttons in recyclerview items? If so, dont use interface, there is an easier way. Use Eventbus, if you dont know how to use it I can add an answer for you! – JDevoloper Oct 31 '19 at 17:01

2 Answers2

0

It is a little confusing radio buttons to behave like checkboxes, but if you really need that, than yes, it is possible.

Do not put them all in a RadioGroup.

Either put them in several groups or manage them yourself completely.

0

First of all, setup your RadioButtons with checked change listeners.

private fun setup() {
    val b1 : RadioButton = findViewById(R.id.radio)
    val b2 : RadioButton = findViewById(R.id.radio)

    b1.setOnCheckedChangeListener { button: CompoundButton, b: Boolean ->
        sendResults()
    }
    b1.setOnCheckedChangeListener { button: CompoundButton, b: Boolean ->
        sendResults()
    }
}

And then just create function for starting new Activity with results.

private fun sendResults() {
    val intent : Intent = Intent(this, MainActivity::class.java)
    intent.putExtra("isChcked1", b1.isChecked())
    intent.putExtra("isChcked2", b2.isChecked())
    startActivity(intent)
}
GensaGames
  • 5,538
  • 4
  • 24
  • 53
  • This looks very well, I did it another way...I added the views dynamically and set an onlclicklistener and used an arraylist to maintain two checks at most and refreshed the radiobuttons on every click to refresh their states. Anyways thank you so much for your answer. – Pemba Tamang Nov 01 '19 at 03:43