Skip to content Skip to sidebar Skip to footer

Getting Radio Button Value From Custom List In Android

I have a custom listview and a radio button in each row, it works properly but i want to reach id of selected radio button from this code. For example when i need the textview1 val

Solution 1:

I want to reach id of selected radio button from this code.

You have to play with setTag & get getTag property to fetch actual id of your selected radio button,

holder.button.setTag(position);  

holder.button.setOnClickListener(new View.OnClickListener() {  
                publicvoidonClick(View v) {   
                 int pos = (Integer) v.getTag();
                 Log.i("ID of radiobutton","Order Edit @ position : " + pos); 
                 String _Str1=array1.get(pos);
                 String _Str2=array2.get(pos);
                 }       
                });  

This way you will get the exact values of textview from your rows.

Solution 2:

Assuming that you need to access the TextView in the onClick method,

@OverridepublicvoidonClick(View v) {
    if((position != mSelectedPosition && mSelectedRB != null)){
        mSelectedRB.setChecked(false);
    }
    mSelectedPosition = position;
    mSelectedRB = (RadioButton)v;
    ViewcontainerView= (View)v.getParent();
    TextViewtextView1= (TextView)containerView.findViewById(R.id.TextView01);
}

EDIT:

I see that you have declared the holder object as final. In that case, you can directly access the holder.text1 and holder.text2 objects from within the event listener.

Solution 3:

Code like this in your button onClick

int selected_radioISAwayGroup = holder.radioISAwayGroup.getCheckedRadioButtonId();
                holder.radioISAwayButton = (RadioButton) findViewById(selected_radioISAwayGroup);

                System.out.println("holder.radioISAwayButton:"+holder.radioISAwayButton.getText().toString());
                if(holder.radioISAwayButton.getText().toString().equalsIgnoreCase("Pre"))
                {
                    //Count +1 for presents
                }
                elseif(holder.radioISAwayButton.getText().toString().equalsIgnoreCase("Abs"))
                {
                    //Count +1 for Absents
                }
                else
                {
                    //Count +1 for Half
                }

Post a Comment for "Getting Radio Button Value From Custom List In Android"