在Android中的ExpanableListView上附加侦听器

在Android中的ExpanableListView上附加侦听器,android,Android,我是android开发的新手,拿到学士学位后找到了一份工作。 几天前,我开始在android上工作,因为我的团队负责人给我分配了与GUI相关的任务。我从我的一个朋友那里听说了这个网站,我在android中遇到了一个可扩展列表视图的问题。我举了一个谷歌搜索的示例代码,我在下面发布。 我需要帮助,我可以添加一个听众,因为我点击组儿童。 我想使用BaseExpandableListAdapter。使用此命令,我无法附加子单击侦听器 有什么想法吗 import android.app.Expandab

我是android开发的新手,拿到学士学位后找到了一份工作。 几天前,我开始在android上工作,因为我的团队负责人给我分配了与GUI相关的任务。我从我的一个朋友那里听说了这个网站,我在android中遇到了一个可扩展列表视图的问题。我举了一个谷歌搜索的示例代码,我在下面发布。 我需要帮助,我可以添加一个听众,因为我点击组儿童。 我想使用BaseExpandableListAdapter。使用此命令,我无法附加子单击侦听器

有什么想法吗

 import android.app.ExpandableListActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListAdapter;
    import android.widget.TextView;
    import android.widget.ExpandableListView.OnChildClickListener;
    public class ExpandableList1 extends ExpandableListActivity implements
        OnChildClickListener {
    ExpandableListAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);
        getExpandableListView().setOnChildClickListener(this);
    }
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        private String[] groups = { "People Names", "Dog Names", "Cat Names",
                "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } };

        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 TextView getGenericView() {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 64);
            TextView textView = new TextView(ExpandableList1.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }
        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) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
        public boolean hasStableIds() {
            return true;
        }
    }
}

您可以将OnChildClickListener用于ExpandableListView

ExpandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    public void onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Object e = (Object)adapter.getChild(groupPosition, childPosition);
        //doing some work for child
    }
}  
说真的,你最好先搜索android参考资料