Problem with expandable list adapter

假装没事ソ 提交于 2019-12-04 17:02:16

I figured it out. For anyone interested, all you have to do is put the "Context" declaration inside "MyExpandableListAdapter", and add "this" to the adapter declaration. See my code below:

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.BaseExpandableListAdapter;
import android.view.LayoutInflater;
import android.content.Context;

public class hmenyActivity extends MyActivity {

    ExpandableListAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hmeny);

        adapter = new MyExpandableListAdapter(this);

        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);

        listView.setOnChildClickListener(new OnChildClickListener()
        {
            @Override
            public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
            {
            Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
            return false;
            }

        });


        listView.setAdapter(adapter);

    }

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {

        private String[] groups = {
                "Test1",
                "Test2",
                "Test3"
            };

        private String[][] children = {

                {
                    "foo1",
                    "foo2",
                    "foo3"
                },

                {
                    "foo1",
                    "foo2",
                    "foo3"
                },

                {
                    "foo1",
                    "foo2",
                    "foo3"
                }
            };

    //Add these lines to your code    
    private Context context;

    public MyExpandableListAdapter(Context context) {
        this.context = context;
    }

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

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

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

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

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.underkat_layout, null);
            }

            TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
            textView.setText(getChild(groupPosition, childPosition).toString());
            return convertView;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
                return groups.length;
        }

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

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.kat_layout, null);
            }
            TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
            textView.setText(getGroup(groupPosition).toString());
            return convertView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

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