Getting radio button value from custom list in android
Asked Answered
V

3

1

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 value of selected row, how can i gather it? Thanks in advance. Here is my code:

private static class Adapter extends BaseAdapter {
         private LayoutInflater mInflater;
         private int mResourceId = 0;
         private RadioButton mSelectedRB;
         private int mSelectedPosition = -1;


         public CitizenAdapter(Context context) {
                   mInflater = LayoutInflater.from(context);
                    }
         public int getCount() {
                    return array.size();
                    }
         public Object getItem(int position) {
                        return position;
                    }
         public long getItemId(int position) {
                        return position;
                    }       
         public View getView(final int position, View convertView, ViewGroup parent) {
                final ViewHolder holder;
                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.table_row_citizen, null);
                        holder = new ViewHolder();
                        holder.text1 = (TextView) convertView.findViewById(R.id.TextView01);
                        holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);                            
                        holder.button = (RadioButton)convertView.findViewById(R.id.radioButtonCitizen);                                                     

                        convertView.setTag(holder);

                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }       
                   holder.text1.setText(array1.get(position));
                   holder.text2.setText(array2.get(position));

                   holder.button.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if((position != mSelectedPosition && mSelectedRB != null)){
                             mSelectedRB.setChecked(false);
                         }
                         mSelectedPosition = position;
                         mSelectedRB = (RadioButton)v;
                     }
                 });

                     if(mSelectedPosition != position){
                         holder.button.setChecked(false);
                     }else{
                         holder.button.setChecked(true);
                         if(mSelectedRB != null && holder.button!= mSelectedRB){
                             mSelectedRB = holder.button;
                         }
                     }                      
            return convertView;
           }    

             static class ViewHolder {      
                  TextView text1;
                  TextView text2;                     
                  RadioButton button;
                }               
            }
Vasodilator answered 1/2, 2013 at 6:36 Comment(2)
id? you mean position of selected radio button? and on click of radio button you are trying to fetch text1 value or text2 value,is it?Casque
exactly, i need to fetch it and use it outside of this classVasodilator
C
0

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() {  
                public void onClick(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.

Casque answered 1/2, 2013 at 12:37 Comment(0)
F
0

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

@Override
public void onClick(View v) {
    if((position != mSelectedPosition && mSelectedRB != null)){
        mSelectedRB.setChecked(false);
    }
    mSelectedPosition = position;
    mSelectedRB = (RadioButton)v;
    View containerView = (View)v.getParent();
    TextView textView1 = (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.

Foreword answered 1/2, 2013 at 6:48 Comment(0)
C
0

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() {  
                public void onClick(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.

Casque answered 1/2, 2013 at 12:37 Comment(0)
M
0

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
                }
                else if(holder.radioISAwayButton.getText().toString().equalsIgnoreCase("Abs"))
                {
                    //Count +1 for Absents
                }
                else
                {
                    //Count +1 for Half
                }
Mangum answered 11/2, 2014 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.