1

我通过扩展 BaseExpandableListAdapter 来填充扩展列表视图。
ChildView 的代码:

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

LayoutInflater in=getLayoutInflater();
convertView=in.inflate(R.layout.details, null);
//set text for various TextViews from ArrayList1 
return convertView;
}

GroupView 的代码:

public View getGroupView(final int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
LayoutInflater li= getLayoutInflater();
convertView=li.inflate(R.layout.list, null);
//add data to ArrayList1 and set text for various TextViews
return convertView;
}

如果一个组被展开并且我长按任何后续组,onItemLongClick 方法将获取参数 itemIndex 的(实际索引 + 1)值。因此,我得到 arrayindexoutofbounds 异常。

elview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int itemIndex, long arg3) {
Log.e("index selected", itemIndex+"");
return false;
}});

我在这里错过了什么吗?

4

1 回答 1

2

我没有看到您为上下文菜单注册适配器...

这是我如何毫无问题地完成它的部分:

适配器创建

    mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
            R.layout.explistlayout,
            R.layout.explistlayout1,
            new String[] { "_id" },
            new int[] { android.R.id.text1 },
            new String[] { child },
            new int[] { android.R.id.text1 });
    lv.setAdapter(mAdapter);
    registerForContextMenu(lv);

上下文菜单和操作

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    if (type == 1) {
        menu.setHeaderTitle("Event Operations");
        menu.add(0, v.getId(), 0, "Edit Event");
        menu.add(0, v.getId(), 0, "Delete Event");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
    if (item.getTitle() == "Edit Event") {
        mRowId = info.id;
        EventAddFragment eventadd = new EventAddFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.rightpane, eventadd).commit();
    } else if (item.getTitle() == "Delete Event") {
        mDbHelper.deleteEvent(info.id);
        mGroupsCursor.requery();
        return true;
    }
    return super.onContextItemSelected(item);
}

您还可以查看此 SO question以获取更多详细信息。

希望这可以帮助。

于 2012-04-29T06:00:29.023 回答