How to get checked radio button in list view?

前端 未结 3 671
广开言路
广开言路 2021-02-06 20:17

I want to get checked radio button from list view from list item like below image.

\"enter

相关标签:
3条回答
  • 2021-02-06 20:25

    I was making project for my collage attendence and i had faced same trouble as you , I solved these problem by just putting these code in my activity code is :-

    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              int child=listView.getChildCount();
              for(int i=0;i<child;i++) {
                 View rgg=listView.getChildAt(i);
    
                 radioGroup = (RadioGroup) rgg.findViewById(R.id.radio);
    
                 int selectedId=radioGroup.getCheckedRadioButtonId();
    
                 radioButton = (RadioButton) rgg.findViewById(selectedId);
    
    }
                }
            });
    
    0 讨论(0)
  • 2021-02-06 20:28

    Problem solved just changing in adapter class.

     package com.cfms.ApplicationLayer;
    
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.LinearLayout.LayoutParams;
        import android.widget.ListView;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;
        import android.widget.RadioGroup.OnCheckedChangeListener;
        import android.widget.TextView;
    
        import com.cfms.cfms_project.R;
    
        public class RadioGroupAdapter extends BaseAdapter implements OnClickListener{
    
                private Context mContext;
                //TextView pretext;
                private int selected = -1;
                int countPre,countAbs,countHD;
                public RadioGroupAdapter(Context c){
                    mContext = c;
    
                }
    
                public int getCount(){
                  return mThumbIds.length;
                }
    
                public Object getItem(int position){ 
                   return null;
                }
    
                public long getItemId(int position){
                   return 0;
                }
    
                public View getView(int position, View convertView, ViewGroup parent){
    
                    ViewHolder holder = null; 
    
    
                 if (convertView == null){
                     LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
                     convertView= infalInflater.inflate(R.layout.employee_attendance_row, null);
                     holder = new ViewHolder();
                     holder.txt=(TextView)convertView.findViewById(R.id.txt_emp_attendance);
                     holder.radiogroup=(RadioGroup)convertView.findViewById(R.id.radio_attendance);
                     holder.pre=(RadioButton)convertView.findViewById(R.id.radio_present);
                     holder.abs=(RadioButton)convertView.findViewById(R.id.radio_absent);
                     holder.halfday=(RadioButton)convertView.findViewById(R.id.radio_half_day);
                    //AttendanceAttandanceView.totalpre=(TextView).findViewById(R.id.txtv_total_pre);
    
                    // System.out.println("index of radio.........."+radioId);
    
                     convertView.setTag(holder);
    
                    }
                     else
                     {
                         holder = (ViewHolder) convertView.getTag();
                     }
    
    
                 holder.radiogroup.setOnCheckedChangeListener(new  OnCheckedChangeListener() {
    
    
    
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        // TODO Auto-generated method stub
                        View radioButton = group.findViewById(checkedId);
                         int radioId = group.indexOfChild(radioButton);
    
    
    
                        if(radioId == 0)
                        {
                            countPre++;
                            AttendanceAttandanceView.totalpre.setText(""+countPre);
    
                        }
    
                        else if(radioId == 1)
                        {
                            countAbs++;
                            AttendanceAttandanceView.totalabs.setText(""+countAbs);
                        }
    
                        else if(radioId == 2)
                        {
                            countHD++;
                            AttendanceAttandanceView.totalHalfday.setText(""+countHD);
                        }
    
    
                        //selected = radioId;
                        //notifyDataSetChanged();
    
                    }
                });
    
    
    
    
    
    
    
                holder.txt.setText(mThumbIds[position]);
                holder.pre.setTag(position);
               // holder.pre.setChecked(position == selected);
    
                holder.abs.setTag(position);
                //holder.abs.setChecked(position == selected);
    
                holder.halfday.setTag(position);
               //holder.halfday.setChecked(position == selected);
    
                convertView.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT ,60));
                return convertView;
    
    
            }
    
            public static String[] mThumbIds = {"EMP1",
                "abcdef ghijklm nopqrstu vwxyz ABCDEFGH IJKLM",
                "EMP3",
                "EMP4",
                "EMP5",
                "EMP6",
                "EMP7",
                "EMP8",
                "EMP9",
                "EMP10",
                "EMP11",
                "EMP12",
                "EMP13",
                "EMP14",
                "EMP15",
                "EMP16",
                "EMP17",
                "EMP18",
                "EMP19",
                "EMP20",
                "EMP21",
                "EMP22"};
    
            static class ViewHolder {
                      TextView txt;
                      RadioGroup radiogroup;
                      RadioButton pre,abs,halfday;
    
                     }
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
            }
    
    
            @Override
            public int getViewTypeCount() {                 
                          //Count=Size of ArrayList.
                return mThumbIds.length;
            }
    
            @Override
            public int getItemViewType(int position) {
    
                return position;
            }
    
    
                } 
    

    Thanks guys for attention.

    0 讨论(0)
  • 2021-02-06 20:45
    public void onRadioButtonClick(View v) {
        RadioButton rb = (RadioButton) v;
        Toast.makeText(getApplicationContext(),
        rb.getText(),Toast.LENGTH_SHORT).show();
        View Rg = (View) rb.getParent();
        View LLayout = (View) Rg.getParent();
        TextView txtLvId = (TextView) LLayout.findViewById(R.id.ins_itemid);
        String LvID = txtLvId.getText().toString();
        Toast.makeText(getApplicationContext(), LvID, Toast.LENGTH_LONG).show();
    }
    
    0 讨论(0)
提交回复
热议问题