user2732743
user2732743

Reputation: 29

How can get the value of Radio Button Inside Listview Click on Button in Android

I have Radio Group of Three Radio Button and a submit button also inside of ListView. I want to get the Button listener and also get the value of selected radio button. Below to my listview item view. please help me.

enter image description here

Upvotes: 0

Views: 866

Answers (3)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You will need to write onClickListener in your custom adapter's getView method:

public View getView(final int position, View convertView,ViewGroup parent) 
{

    Button submit= (Button) convertView.findViewById(R.id.submit);

    submit.setOnClickListener(new OnClickListener() 
    { 
        @Override
        public void onClick(View v) 
        {
            RadioGroup rGroup = (RadioGroup)convertView.findViewById(R.id.radiogroup);
            int checkedID = rGroup.getCheckedRadioButtonId();
            if(checkedID >=0 )
                RadioButton rButton = (RadioButton) convertView.findViewById(checkedId);
            else{
                //no radio button was checked
            }
        }  

    });
    return convertView ;
}

Hope this helps.

Upvotes: 1

Dilip
Dilip

Reputation: 2311

put this code inside click event

if(rg1.getCheckedRadioButtonId()!=-1){
    int id= rg1.getCheckedRadioButtonId();
    View radioButton = rg1.findViewById(id);
    int radioId = radioGroup.indexOfChild(radioButton);
    RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
    String selection = (String) btn.getText();
}

Upvotes: 1

Shriram
Shriram

Reputation: 4421

RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);

        group.setOnCheckedChangeListener(new OnCheckedChangeListener() 
           {

            public void onCheckedChanged(RadioGroup group, int checkedId) 
               {
                // TODO Auto-generated method stub
                if(radiobutton1.isChecked())
                  {
                    //your code
                  }
                else if(radiobutton2.isChecked())
                  {

                     //your code
                  }
             }
        });

Upvotes: 0

Related Questions