如何获取在ExpandableListView中单击的对象?

时间:2013-10-09 09:06:48

标签: android

我想知道,如何获取在ExpandableListView

中点击的对象

我有一个BaseExpandableListAdapter,这是我的方法:

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final Foo children = (Foo) getChild(groupPosition, childPosition);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    // view stuff

    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // how to get the clicked child object of type Foo?
        }
    });

    convertView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // also how to get it here?
            return true;
        }
    });

    return convertView;
}

如何为点击和长按一下监听器获取Foo类型的Child元素?

1 个答案:

答案 0 :(得分:0)

在代码中使用onChildClick方法。单击子项时会自动调用此方法。使用this链接进行长按。

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

            return true;
        }