1

I have radio buttons in listview, for every column a radio button and a checkbox will added at the end. But user should be able select only one radio button in the list shown, i tried looking in net where all i can get how to list radio buttons using adapter which has been done already by me. Advance thanks. Below is my code:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view;
        ViewHolder holder;
        int selected;
        ListCollection person = items.get(position);
        if(convertView == null) {
            view = mInflater.inflate(R.layout.contact_adapter, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView)view.findViewById(R.id.user_name);
            //holder.primary = (RadioButton) view.findViewById(R.id.primary_radio);
            holder.inVoice = (CheckBox) view.findViewById(R.id.checkBox1);
            holder.radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder)view.getTag();
        }

        holder.name.setText(person.text);

        selected  = holder.radioGroup.getCheckedRadioButtonId();

        holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                int childCount = group.getChildCount();
                 for (int x = 0; x < childCount; x++) {
                     RadioButton btn = (RadioButton) group.getChildAt(x);
                     if (btn.getId() == checkedId) {

                     }
                 }
            }
        });
            RadioButton button = new RadioButton(context);   
             button.setText("Primary");
             button.setid(i);
             holder.radioGroup.addView(button);
        return view;
    } 
Vicky
  • 921
  • 1
  • 11
  • 33
  • can you post xml here? – Nilay Dani Jul 13 '15 at 16:23
  • well i didn't add radio button in my xml i have implemented in coding above in my xml i have only radiogroup, if i add radio button inside group , every button will gets on radio group what i need is all buttons under one radio group. – Vicky Jul 13 '15 at 16:31

2 Answers2

0

I think your answer is here

How to use RadioGroup in ListView custom adapter?

Community
  • 1
  • 1
sadegh saati
  • 1,168
  • 1
  • 13
  • 24
0

You need to do two things:

  1. Use mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  2. Make your custom row view implement Checkable.
  • i have multiple rows, each row containing one textview, checkbox and radiobutton. User should be able to select only one radiobutton while he can select multiple checkbox in the listview. – Vicky Jul 14 '15 at 04:57
  • it doesn't work, can you suggest any other idea or any solution. – Vicky Jul 15 '15 at 05:10