在自定义可扩展列表适配器中扩展视图时遇到问题

having trouble inflating view in custom expandable list adapter

我在 getGroupView() 中创建了自定义可扩展列表适配器 和 getChildView() 我做了以下但收到错误:

        public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater()
                    .inflate(R.layout.expandable_list_parent_row_layout);
        }
        return convertView;
    }

编译器告诉我它找不到方法 - getActivity()

有人可以帮忙吗?谢谢!

在适配器构造函数中,您将有一个 Context 参数。使用此 (Activity) 上下文获取 LayoutInflater。

适配器 class 没有 getActivity() 方法。

您需要 post 更多代码 - 我怀疑您是在 Activity 而不是片段(提供方法 getActivity())中访问它

您需要在代码中使用 Context,这就是您的代码的样子。

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);