Implement RadioButton in ListView

0

As you can see in the image I have added radio buttons in my list, but I only want one to be selected, I am using simpleCursorAdapter

I can select an item in the following way and without problem adding to my variable the name of the selected item.

....
String folio;  
Seleccionado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView infofolio = (TextView)view.findViewById(R.id.txtFolio);
                TextView infohuerto = (TextView)view.findViewById(R.id.txtHuerto);

                    folio = infofolio.getText().toString();


            }
        });

but how could I use the part of the radiobuttons and that only one can be selected and pass the name to my variable folio?

    
asked by DoubleM 12.02.2017 в 08:10
source

1 answer

1

Try this in your XML

    <RadioButton 
       ...
       android:onClick="onClickRadioButton"
       ...
     />

and this in Java:

       private RadioButton listRadioButton = null;
       int listIndex = -1;

    public void onClickRadioButton(View v) {
    View vMain = ((View) v.getParent());

    if (listRadioButton != null) listRadioButton.setChecked(false);
    listRadioButton = (RadioButton) v;
    if (listRadioButton.isChecked) {
        listIndex = ((ViewGroup) vMain.getParent()).indexOfChild(vMain); 
    /*tu logica aqui folio = infofolio.getText().toString();
    TextView infofolio = (TextView)view.findViewById(R.id.txtFolio);
    TextView infohuerto = (TextView)view.findViewById(R.id.txtHuerto);
    */
    } else {
        listRadioButton = null;
        listIndex = -1
    }
}
    
answered by 12.02.2017 / 20:18
source