如何从ExpandableListView获取特定视图?

时间:2019-05-28 17:57:36

标签: java android expandablelistview

我创建了一个可扩展的列表视图,并在其中创建了两个单选按钮。 我想获取在我的主活动中按下某个按钮时选中的单选按钮的值。我的ExpandableListView工作正常,但是我无法获取在主要活动中选择的单选按钮。

public class ExpandableMenuListAdapter  extends BaseExpandableListAdapter
 {
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<List<String>>> listHashMap;

    public ExpandableMenuListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<List<String>>> listHashMap) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;

    }

    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return Objects.requireNonNull(listHashMap.get(listDataHeader.get(groupPosition))).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return Objects.requireNonNull(listHashMap.get(listDataHeader.get(groupPosition))).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = (String)(getGroup(groupPosition)).toString();
        if (convertView==null)
        {
            LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_menu_days_group,null);

        }
        TextView lblListHeader = convertView.findViewById(R.id.list_days);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        List<String> childText = (List<String>) getChild(groupPosition,childPosition);
        if (convertView==null)
        {
            LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_menu_item,null);
        }
        RadioButton txtListChild = convertView.findViewById(R.id.menu_list_daily1);
        RadioButton txtListChild2 = convertView.findViewById(R.id.menu_list_daily2);
        txtListChild.setText(childText.get(0));
        txtListChild2.setText(childText.get(1));
        ViewsList.menu.add(convertView);


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

在代码中使用onChildClick方法。单击子项时会自动调用此方法

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    //get your radio here from the view v and do whatever you want 

    return true;
}