androidlistviewradio-buttonexpandablelistviewexpandablelistadapter

How to handle Radio Button in ExpandableListView?


I have an ExpandableListView with more than or equal to 2 groups. In those groups, there are few items with radio buttons. So, when I select one radio button from one group, then other buttons from another group should not be selected.

Following code helps to get the radio button work as the radio group.

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.attribute_expanable_child_item, null);
    }

    RadioButton mRadioButton = convertView.findViewById(R.id.radio_option);
    mRadioButton.setVisibility(View.VISIBLE);
    mRadioButton.setText(mItem.getAttributeName());

    mRadioButton.setChecked(childPosition == selectedPosition);
    mRadioButton.setTag(childPosition);

    mRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedPosition = (Integer) view.getTag();
            notifyDataSetChanged();
        }
    });
}

Right now, if I select one item from one group, another item from another group gets selected at same position! I know I should be saving the selected positions in an array, but I'm not getting how. So, what should I do to achieve this for multiple groups in ExpandableListView?


Solution

  • I found the solution way back, but was busy with the Project so posting it now, Considering someone can get help from it!

    In order to maintain the selected position respective to that particular group in expandable ListView, I have to save that position! So I saved it using HashMap.

    So in the first parameter of hashmap, I stored the group position and in the second view's ID. And set it when the expandable's child is being populated.

    So final code is as follows:

    Hashmap<Integer, Integer> mChildCheckStates = new Hashmap<>();
    
    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
    
        final AttributePOJO mItem = (AttributePOJO) getChild(groupPosition, childPosition);
        String mGroup = (String) getGroup(groupPosition);
    
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.attribute_expanable_child_item, null);
        }
    
        RadioButton mRadioButton = convertView.findViewById(R.id.radio_option);
        mRadioButton.setVisibility(View.VISIBLE);
    
        try {
            mRadioButton.setChecked(childPosition == mChildCheckStates.get(groupPosition));
    
        }catch (Exception e){
            e.printStackTrace();
        }
        mRadioButton.setTag(childPosition);
    
        mRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mChildCheckStates.put(groupPosition, (Integer) view.getTag());
                notifyDataSetChanged();
            }
        });
    
        return convertView;
    }
    

    And that's it!