Checking RadioButton of RadioGroup in ListView checks other item button

半世苍凉 提交于 2019-12-06 15:36:39

You need some way of setting the checked state for each RadioButton to distinguish if it's in the correct row or not. You can use getTag()/setTag() for that. I have an ExpandableListView so I store the child row id that I have in a separate "child" class for each object.

So I have something like holder.myRG.setTag(childId); after first getting that id at the beginning of getView(). You could probably use the position.

Before setting this, I set my RadioGroups checked button to -1 so nothing is checked each time then I check a HashMap<String, Boolean> to see if that item is there and if it is true. Items are added to the map in onCheckedChanged() if it is checked.

Here is an example of what I'm doing.

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, 
            View convertView, ViewGroup parentView)
{             
    final FormItemHolder holder;
    View view = convertView;

    // here I get the id
    final String childId = mList.get(groupPosition).getChildren()
            .get(childPosition).mId; 

    // I set the listener to null and uncheck both buttons--you may not need this
    holder.rgCompliance.setOnCheckedChangeListener(null);
    holder.rgCompliance.check(-1);

    // in my listener, I add a reference to the button in a map declared in another class
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    { 
        String uid = (String) holder.myRG.getTag();                                 
        currentInspectionFormItem = InspectionFormData.getItemById(uid);
        if (checkedId == R.id.rbCompliant)
        {
            myMap.put(uid, true);
        }                   
        else if ((checkedId == R.id.rbNotCompliant && show))
        {
            if (myMap.get(uid) == null 
                    || (InspectionFormData.complianceList.get(uid) != null 
                     && InspectionFormData.complianceList.get(uid)))
             {
                myMap.put(uid, false);
             }
         }  
       }        
    });

    // here I check to see if the buttons id has been put in the list
    // if it is present then I check the button accordingly
    Boolean present = myMap.get(childId);
    if (present != null)
    {   
        if (holder.myRG.getCheckedRadioButtonId() != -1)
            holder.myRG.setOnCheckedChangeListener(null);
        holder.myRG.check((present.booleanValue()) ? holder.rb1.getId() : holder.rb2.getId());                                                          
    }
} 

I'm doing a lot more than what's shown but I think I pulled out the relevant parts.

I had also faced same problem , but i had solved it ,I think you can just use code which is in same way as the code i am writing code is :- ///done is your final calculation button

done.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         final 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);

}
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!