ExpandableListView with RadioButtons. Choose a value for one radio group, choose a few more when the group expands

I am replicating a validation form for a piece of hardware in an android app. There is an ExpandableListView where the group data is composed of different machine and child data systems, contains an item checklist with a RadioGroup for technicians to indicate the pass / fail status of an item. Everything works fine, except that when choosing a value for a RadioButton in one child, it selects a value for the same RadioButton in other groups. Therefore, if I had:

Group 1
  Question 1
    Passed
    Failed
    Corrected
    NA
  Question 2
    Passed
    Failed
    Corrected
    NA
Group 2
  Question 1
    Passed
    Failed
    Corrected
    NA
  Question 2
    Passed
    Failed
    Corrected
    NA

      

If I select Sent in both questions in group 1 without going to group 2, expanding 1 or both of RadioGroups will also select Missed.

The layout I am using to create a child group with RadioGroup looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip">
<TextView
android:id="@+id/questionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:tag="actionGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
  android:tag="passed"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Passed"/>
<RadioButton
  android:tag="failed"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Failed"/>
<RadioButton
  android:tag="corrected"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Corrected"/>
<RadioButton
  android:tag="na"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="NA"/>
</RadioGroup>
</LinearLayout>

      

BaseExpandableListAdapter Activation

ExpandableListView inspectionQuestions = (ExpandableListView) FindViewById(Android.Resource.Id.List);
inspectionQuestions.SetAdapter(new ExpAdapter(this));

      

BaseExpandableListAdapter

public class ExpAdapter : BaseExpandableListAdapter
{
    private readonly Context context;
    Questions questions = new Questions();
    private int seed = 1000;

    public ExpAdapter(Context ctx)
    {
        context = ctx;
    }

    public override Object GetChild(int groupPosition, int childPosition)
    {
        return null;
    }

    public override long GetChildId(int groupPosition, int childPosition)
    {
        return 0;
    }

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.inspection_row_2, null);
        }

        int currentID;

        TextView question = (TextView) convertView.FindViewById(Resource.Id.questionText);
        RadioGroup radio = (RadioGroup) convertView.FindViewWithTag("actionGroup");
        radio.Id = seed++;
        RadioButton pass = (RadioButton) convertView.FindViewWithTag("passed");
        pass.Id = seed++;
        RadioButton fail = (RadioButton) convertView.FindViewWithTag("failed");
        fail.Id = seed++;
        RadioButton correct = (RadioButton)convertView.FindViewWithTag("corrected");
        correct.Id = seed++;
        RadioButton na = (RadioButton)convertView.FindViewWithTag("na");
        na.Id = seed++;

        string[][] items = questions.childItems();
        question.Text = items[groupPosition][childPosition];

        return convertView;
    }

    public override int GetChildrenCount(int groupPosition)
    {
        return questions.childItems()[groupPosition].Length;
    }

    public override Object GetGroup(int groupPosition)
    {
        return null;
    }

    public override long GetGroupId(int groupPosition)
    {
        return 0;
    }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem1, null);
        }

        TextView groupName = (TextView) convertView.FindViewById(Android.Resource.Id.Text1);

        QuestionGrouper grouper = new QuestionGrouper();
        groupName.Text = grouper.groupItems()[groupPosition];

        return convertView;
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    public override int GroupCount
    {
        get { return 9; }
    }

    public override bool HasStableIds
    {
        get { return false; }
    }
}

      

EDITED GetChildView per suggestion:

   public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.inspection_row_2, null);
        }

        TextView question = (TextView) convertView.FindViewById(Resource.Id.questionText);

        RadioGroup radio = (RadioGroup) convertView.FindViewById(Resource.Id.activityGroup);
        int count = radio.ChildCount;

        if (count == 4)
        {
            return convertView;
        }

        RadioButton pass = new RadioButton(context) {Text = "Passed"};
        RadioButton fail = new RadioButton(context) {Text = "Failed"};
        RadioButton correct = new RadioButton(context) {Text = "Corrected"};
        RadioButton na = new RadioButton(context) {Text = "NA"};

        radio.AddView(pass);
        radio.AddView(fail);
        radio.AddView(correct);
        radio.AddView(na); 

        string[][] items = questions.childItems();
        question.Text = items[groupPosition][childPosition];

        return convertView;
    }

      

+3


source to share


3 answers


Are you sure the @ + value you use for each RadioButton is different for each group? This is the first thing that comes to mind. Even if they are in a different RadioGroup, I'm pretty sure they need to have unique IDs so that you can find findViewById correctly.



0


source


You most certainly shouldn't be adding a radio children group from xml .. try adding it at runtime ... then just save everything

these children in some key value pair (child position as key and RadioButton object). Now you don't

need uniqueness since you have a link to all RadioButtons. I had the same problem and I did this, now she



works great .. they are easy to deal with as you always get childPosition in

ExpandibleListView , making it easier to access.

0


source


Use ListView and Inflate for each item. See this link: RadioGroup in ListView

0


source







All Articles