我通过扩展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;
}});
我在这里错过了什么吗?
答案 0 :(得分: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);
}
您还可以查看this SO question了解详情。
希望这有帮助。